2 JavaScript Statements and Loop
2 JavaScript Statements and Loop
2 JavaScript Statements and Loop
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
if (condition) {
// block of code to be executed if the condition is true
}
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false
and condition2 is true
} else {
// block of code to be executed if the condition1 is false
and condition2 is false
}
example-
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript if .. else</h2>
<p id="demo"></p>
<script>
let greeting;
} else {
document.getElementById("demo").innerHTML = greeting;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript switch</h2>
<p id="demo"></p>
<script>
let day;
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
</script>
</body>
</html>
JavaScript Loops
Loops can execute a block of code a number of times.
Loops are handy, if you want to run the same code over and over again, each time with a different value.
Often this is the case when working with arrays:
Instead of writing:
text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For Loop</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For In Loop</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
Example Explained
The for in loop iterates over a person object
Each iteration returns a key (x)
The key is used to access the value of the key
The value of the key is person[x]
It lets you loop over iterable data structures such as Arrays, Strings,
Maps, NodeLists, and more:
Browser Support
For/of was added to JavaScript in 2015 (ES6)
while (condition) {
// code block to be executed
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript While Loop</h2>
<p id="demo"></p>
<script>
let i = 0;
i++;
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
do {
// code block to be executed
}
while (condition);
The example below uses a do while loop. The loop will always be executed at least once, even if the
condition is false, because the code block is executed before the condition is tested:
<html>
<body>
<p id="demo"></p>
<script>
let text = ""
let i = 0;
do {
i++;}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Do not forget to increase the variable used in the condition, otherwise the loop will never end!