Conditionals
Conditionals take an expression, which is code that evaluates to determine a value, and checks if it is true
or false
. If it’s true
, the program can be told to do one thing and if it’s false
, the program can be told to do another thing.
If Statement
An if
statement accepts an expression with a set of parentheses:
- If the expression evaluates to a truthy value, then the code within its code body executes.
- If the expression evaluates to a falsy value, its code body will not execute.
const isMailSent = true;if (isMailSent) {console.log('Mail sent to recipient 💌');}
The output would be:
Mail sent to recipient 💌
Note: if
is in lowercase letters. Uppercase letters will generate a JavaScript error.
Else Statement
An else
block can be added to an if block or series of if-else if blocks. The else
block will be executed only if the if
condition fails.
const isTaskCompleted = false;if (isTaskCompleted) {console.log('Task completed');} else {console.log('Task incomplete');}
If the hour
is less than 18, create a “Good day” greeting, otherwise “Good evening”:
if (hour < 18) {greeting = 'Good day 🌤';} else {greeting = 'Good evening 🌙';}
Else If Statement
After an initial if
block, else if
blocks can each check an additional condition. An optional else
block can be added after the else if
block(s) to run by default if none of the conditionals evaluated to truthy.
If time is less than 10:00, create a “morning” greeting, if not, but time is less than 20:00, create a “day” greeting, otherwise a “Good evening”:
if (time < 10) {greeting = 'Good morning 🌄';} else if (time < 20) {greeting = 'Good day 🌁';} else {greeting = 'Good evening 🌉';}
Here, the result of greeting will be: Good day 🌁
.
Codebyte Example
What do you think will happen when pH
is changed to 7?
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours