How does the else statement work in JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How does the else statement work in JS?

var num1 = prompt("Please enter the first number"); var num2 = prompt("please enter the second number"); if (num1 >= num2) { alert("First Number is greater than Second Number"); } else { alert ("Second Number is greater than First Number"); }

14th Apr 2018, 8:15 PM
nyambura00
nyambura00 - avatar
2 Answers
+ 2
If the condition tested in the if statement is not true, anything code instead of else will run
14th Apr 2018, 8:34 PM
Ariela
Ariela - avatar
+ 1
The else statement works that if the previous if statement is false it will go and run the else block of code. In this case if the num1 variable is smaller than num2 then the else statement will be executed. My example: var variable = 50; if (variable > 100) { alert(variable + " is bigger than 100"); } else { alert(variable + " is smaller than 100"); } This JS script would alert "50 is smaller than 100" because 50 is not bigger than 100
14th Apr 2018, 8:37 PM
TurtleShell
TurtleShell - avatar