-
Notifications
You must be signed in to change notification settings - Fork 2
JS If...Else
The if
statement executes a statement if a specified condition is true. If the condition is false, another statement can be executed using the else
statement..
Note: The
else
statement is optional.
if (condition)
statement1
else
statement2
Multiple if...else
statements can be nested to create an else if
clause.
if (condition1)
statement1
else if (condition2)
statement2
else if (condition3)
statement3
...
else
statementN
Note: If you want to execute more than one statement in the if
, else
or else if
part, braces are required around the statements.
Using if...else
:
if (x == 5) {
z = 7;
q = 42;
}
else
z = 19;
Using else if
:
if (x < 10)
return "Small number";
else if (x < 50)
return "Medium number";
else if (x < 100)
return "Large number";
else {
flag = 1;
return "Invalid number";
}
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links