Then we use the first condition to evaluate if the number is positive. In this example we check if a number is positive, negative or null.ĭocument.writeln('The number is negative') You can also insert other else if in cascade, as many as needed. If condition2 is false, go to else and then execute block3 of instructions. Condition1 is false, then we do not execute the block1 of statements specified in curly brackets and we go on to evaluate condition2.If condition2 is true, block2 of instructions indicated in curly brackets is executed, The second condition and the else are therefore discarded. Condition1 is true, then the block1 of statements specified in curly brackets are executed.block 3 of instructions executed if the condition 1 e 2 are false block 2 of instructions executed if the condition is false block 1 of instructions executed if the condition is true You can also specify multiple conditions using else if, following this syntax: JavaScript if statement – else if cascaded So let’s take an example of an if else statement in JavaScript, where we check if a number is greater than 100 or not.ĭocument.writeln('number is greater than 100') ĭocument.writeln('number is not greater than 100') In this example we check if a number is positive and we write it in the html page with the writeln method.ĭocument.writeln('The number is positive') So if the number is greater than 100, ‘the number is greater than 100’ will be printed on the html page, otherwise ‘the number is not greater than 100’ will be printed. Therefore, if the condition is true, one or more instructions indicated in curly brackets are executed, otherwise the instructions indicated in the else are executed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |