Ad

JavaScript Conditional Statement - if

If statements are used to execute statements based on the condition. If the condition is true, then it executes the block of statements, otherwise executes some other block of statements.

For example: if a number is greater than 0, then we can say that it is a positive number.

let n=Number(prompt("Enter Number:"))
if(n>0){
    console.log('Number is positive');
}
else{
    console.log('Number is negative');
}

Exercise 1: Input 2 numbers and find greater. 1

Exercise 2: Input age and find if it is greater than or equal to 18 or not. 1

Exercise 3: Input a number and find if it is odd or even. 1

Exercise 4: Change Image on Image Click. 1

Exercise 5: Input the amount of shopping and calculate the discount if the amount is greater than 1000, then a 15% discount is given, otherwise 5%. 1

Advertisement Ad

Conditional Operators

The conditional operator is also known as the ternary operator. If-else conditions can also be written using the conditional operator (? :), but all in one line (the condition, true statement, and false statement). Syntax:

let r = condition ? true statement : false statement;

Example: find the greater number.

let greater = (a > b) ? a : b;

Nested Conditional Operator

The conditional operator can be nested the same way as a nested if condition. Syntax:

let r = condition1 ? true statement : condition2 ? true statement : false condition

The above syntax checks the condition, if the condition is true, then it executes the true statement, and if the condition is false, then it goes to condition2, based on condition2, it executes the true or false statement. You can apply as many conditions in nested conditional operators.

More about: if condition

Values like 0, an empty string "", null, undefined, and NaN are false, other than these values became true.

if(true){
    console.log("True")
}else{
    console.log("False")
}//True
if(1){
    console.log("True")
}else{
    console.log("False")
} //True
if("1"){
    console.log("True")
}else{
    console.log("False")
} //True
if(5){
    console.log("True")
}else{
    console.log("False")
} //True
if(false){
    console.log("True")
}else{
    console.log("False")
} //False
if(0){
    console.log("True")
}else{
	console.log("False")
} //False
if("0"){
    console.log("True")
}else{
    console.log("False")
} //True
if("a"){
    console.log("True")
}else{
    console.log("False")
} //True

Exercise 6: Select a country to find its capital using the select option.

Conditional Statement: switch

The switch statement is used to select one of the many blocks of code to be executed. It can be used instead of nested multiple if statements. In switch statement we use case to check values and a optional default statement. Syntax:

switch(n){
    case 1:
        statements;
        [break]
    case 2:
        statements;
        [break]
    [default:]
        statements;
        [break]
}

The value of n is check with the first case value, if it matched, execute the statments inside case 1, otherwise check for next case value, if no case value is matched then default case will be executed. Case value can be of integer, character data type.

If there is no break statement is applied, then it execute all case statement after the matching case value.

for multiple case values, you can group cases like:

case 1:
case 2:
case 3:
    statements
break;

Example 1: Input number and display corresponding day. Like (Monday=1, Tuesday=2, ...).

JavaScript Feedback, Questions, Suggestions, Discussion.

Ad: