- The if else statment
- The switch statment
- The For Loop
The If else statment
The Switch Statment
var day = 2;
switch (day) {
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday");
break;
case 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
// Outputs "Tuesday"
The Loop
For loop
for (i=1; i<=5; i++) {
document.write(i + "<br />");
}
While loop
var i=0;
while (i<=10) {
document.write(i + "<br />");
i++;
}
Do… While loop
var i=20;
do {
document.write(i + "<br />");
i++;
}
while (i<=25);
Break
for (i = 0; i <= 10; i++) {
if (i == 5) {
break; // The break statement "jumps out" of a loop and continues executing the code after the loop.
}
document.write(i + "<br />");
}
Continue
for (i = 0; i <= 10; i++) {
if (i == 5) {
continue; //The continue statement breaks only one iteration in the loop, and continues with the next iteration.
}
document.write(i + "<br />");
}
User Function
function my_function(arg1, arg2){
//...
var result = arg1 + arg2;
return result;
// If you do not return anything from a function, it will return undefined.
}
var print_sum = my_function(3, 2);
document.write(print_sum);
Objects
var person = {
//property:value
name: "Yury",
age: 32,
favColor: "Green",
height: 172,
};
person.name
//or
person['age']
person.name.length
function qwe(name, age, color) {
this.name = name;
this.age = age;
this.favColor = color;
}
var vasia = new qwe('Vasia', 37, 'blue');
var petia = new qwe('Petia', 45, 'red');
document.write(vasia.name)
document.write("<br>")
document.write(petia.name)