Condition
Condition
Condition
What we will learn
i
if els
nested if
if else if ladder
Human Beings make decisions all the time. For example, every morning, we make a decision between eating or
not eating before starting our daily chores. Conditional statements allow us to represent such decision-making
in JavaScript, from the choice that must be made.
JavaScript is a programming language that is commonly used to create interactive and dynamic elements on
websites. One of the key features of JavaScript is the ability to use conditional statements to control the flow of
a program.
Conditions work on boolean values, true or false. It is true if it meets the requirement, false otherwise. That is
expressions (conditions) are evaluated to be either true or false.
The condition is any expression that can be evaluated as true or false. For example, you can use a comparison
operator (such as <, >, ==) to compare two values, or you can use a logical operator (such as &&, ||) to
combine multiple conditions.
Ex:
An if statement can also include an optional else statement, which will execute if the condition is false.
Ex:
Nested if statement:
Ex:
Check if the user is logged in and if the logged in user is admin or not.
JavaScript also supports the use of else if statement, which allows you to chain multiple conditions together
also called if else ladder.
Note 2: Just like the if statement in the if-else ladder also we can omit the last else block, it is not mandatory to
put.
Ex: