Urgent.News

What's breaking now, across thousands of outlets.

Tech

Javascript If-else Statement

The JavaScript if-else statement is used to execute the code whether condition is true or false. There are three forms of if statement in JavaScript. 1.if statement 2.if else statement 3.if else if statement 1.JavaScript if statement It evaluate the content only if expression is true. //Syntax if ( expression ){ //content to be evaluated } //Example code < script > let a = 20 ; if ( a > 10 ){…

The JavaScript if-else statement is employed to execute code based on whether a condition is true or false. There exist three variations of the if statement in JavaScript: 1. if statement, 2. if else statement, and 3. if else if statement.

The if statement is evaluated only if the expression is true. For instance, in the following code snippet, if variable 'a' is greater than 10, then it will display the message "value of a is greater than 10".

```javascript

let a = 20;

if (a > 10) {

document.write("value of a is greater than 10");

}

```

The output of this code would be: "value of a is greater than 10".

The if else statement evaluates the content depending on whether the condition is true or false. For example, in the code below, if 'a' is an even number, it will print "a is even number". However, if 'a' is an odd number, the script will print "a is odd number".

```javascript

let a = 20;

if (a % 2 == 0) {

document.write("a is even number");

} else {

document.write("a is odd number");

}

```

The output would be: "a is even number".

Lastly, the if else if statement evaluates the content based on several expressions. It will display the result of the first true expression. If none of the expressions are true, it will execute the code within the else block. In the following example, if 'a' is equal to 20, it will print "a is equal to 20".

```javascript

let a = 20;

if (a == 10) {

document.write("a is equal to 10");

} else if (a == 15) {

document.write("a is equal to 15");

} else if (a == 20) {

document.write("a is equal to 20");

} else {

document.write("a is not equal to 10, 15 or 20");

}

```

The output of this code would be: "a is equal to 20".

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Friday 28 August →