Beruflich Dokumente
Kultur Dokumente
A DOM-tree comprises
Document Node
the root node representing the entire HMTL document.
Element node
represents an HTML element (or tag). An element node may have
child nodes, which can be either element or text node. Element node
may also have attributes.
• You can access and modify the content of an element via the "innerHTML" property, which
contains all the texts (includes nested tags) within this element
• You can access and modify the content of an element via the "innerHTML" property, which
contains all the texts (includes nested tags) within this element
Activate the browser console with F12, and select "Console" in the menu to see the result
• All JavaScript variables must be identified with unique names called identifiers.
• Identifiers can be
• Short descriptive names (age, sum, Volume).
The general rules for constructing names for variables (unique identifiers) are:
• Names can contain letters, digits, underscores, and dollar signs.
• Names must begin with a letter
• Names can also begin with $ and _
• Names are case sensitive (y and Y are different variables)
• Reserved words (like JavaScript keywords) cannot be used as names
Example : 1
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = z;
Example : 2
var person = “APJ", carName = 'swift', price = 200;
document.getElementById("demo").innerHTML = carName;
Infinity / -Infinity is the value JavaScript will return when a number outside the largest possible number is
calculated
Example : document.write(10/0);
Example:
var msg = "JavaScript is a high-level, dynamic”;
document.write("type :" + typeof msg);
• An array is a special variable, which can hold more than one value at a time
• It is a special type of object
<script>
var cars = [“Merc","Volvo","BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
document.getElementById("demo").innerHTML = fruits.toString();
document.getElementById("demo").innerHTML = fruits.join(" * ");
fruits.pop(); // Removes the last element ("Mango") from fruits
var x = fruits.pop(); // the value of x is "Mango“
fruits.push("Kiwi"); // Adds a new element ("Kiwi") to fruits at the end
var x = fruits.push("Kiwi"); // returns the new array length
fruits.shift(); // Removes the first element "Banana" from fruits
fruits.unshift("Lemon"); // Adds a new element "Lemon" to fruits
fruits[0] = "Kiwi"; // Changes the first element of fruits to "Kiwi“
fruits[fruits.length] = "Kiwi"; // Appends "Kiwi" to fruit
delete fruits[0]; // Changes the first element in fruits to undefined
Example:
<script>
var person;
document.getElementById("demo").innerHTML =person + "<br>" + typeof person;
</script>
Example :
<script>
var carName = “Punto";
var carName;
document.getElementById("demo").innerHTML = carName;
</script>
document.write("5+4=", 5+4);
output : 5 +4 = 9
document.write("5+4=“+5+4);
output : 5 + 4 = 54
Example:
for (i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
while (condition) {
code block to be executed
}
Example :
while (i < 10) {
text += "The number is " + i;
i++;
}
do {
code block to be executed
}
while (condition);
Example :
do {
text += "The number is " + i;
i++;
}
while (i < 10);
• Continue statement breaks one iteration and continues with the next
iteration in the loop
Example:
• label: statements
var cars = ["BMW", "Volvo", "Saab", "Ford"];
list:
• Example {
text += cars[0] + "<br>";
break labelname; text += cars[1] + "<br>";
text += cars[2] + "<br>";
continue labelname; break list;
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";
}
Example:
function myFunction(a, b)
{
return a * b;
}
let r = myFunction(10,20);
• Example
var x = function (a, b) {return a * b};
var z = x(4, 3);
<script>
(function () {
document.getElementById("demo").innerHTML = "Hello! I called myself";
})();
</script>
Rules
• JavaScript function definitions do not specify data types for parameters
• JavaScript functions do not perform type checking on the passed arguments
• JavaScript functions do not check the number of arguments received
• If a function is called with missing arguments (less than declared), the missing values are set to: undefined
• Example
function sumAll() {
var i, sum = 0;
for (i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
<script>
var myObject = {
firstName:“Java",
lastName: “Script",
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
document.getElementById("demo").innerHTML = myObject.fullName();
</script>
<script>
function myFunction(arg1, arg2) {
this.firstName = arg1;
this.lastName = arg2;
}
• Events:
• click: generated when the user clicks on an HTML element.
• mouseover, mouseout : generated when the user positions the mouse pointer inside/away from
the HTML element.
• load, unload: generated after the browser loaded a document, and before the next document is
loaded, respectively.
• Event Handler:
• A piece of program that fires in response to a certain user's or browser's action
• The event handler is typically attached to the target HTML tag, e.g.,
<script>
var person = {
firstName : “MK",
lastName : “Gandhi",
age: 78,
eyeColor : “Hazel"
};
• Encapsulation
• By Defining an Object template
• Most of OOP languages call it a CLASS
• Prototype Chaining