0% found this document useful (0 votes)
2 views5 pages

Condition

This lesson introduces conditional statements in JavaScript, which allow programmers to make decisions based on boolean values. It covers the basic 'if' statement, the 'if-else' statement, nested 'if' statements, and the 'if else if' ladder for chaining multiple conditions. Each type of statement is explained with syntax and examples relevant to programming logic.

Uploaded by

chandrabhanu2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Condition

This lesson introduces conditional statements in JavaScript, which allow programmers to make decisions based on boolean values. It covers the basic 'if' statement, the 'if-else' statement, nested 'if' statements, and the 'if else if' ladder for chaining multiple conditions. Each type of statement is explained with syntax and examples relevant to programming logic.

Uploaded by

chandrabhanu2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lesson:

Condition
What we will learn
i
if els
nested if
if else if ladder

Introduction to conditional statements:


Programming Languages are tools that allow us to write code that instructs the computer to do something. In
every programming language, the code needs to make decisions and carry out actions accordingly
depending on different inputs.

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.

Conditional statements in JavaScript:


if statement:

The most basic form of a conditional statement is the if statement. 

The syntax for an if statement is as follows:

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:

Divide only, when the divisor is != 0

Full Stack Web development


if-else statement:

An if statement can also include an optional else statement, which will execute if the condition is false.

The syntax for an if-else statement is as follows:

Ex:

Only age above 18 are eligible for registration.

Full Stack Web development


Ex:

Allow only admin to fetch user details

Nested if statement:

We can nest if else statements inside another if else statements.

The syntax for an nested if statement is as follows:

Ex: 

Check if the user is logged in and if the logged in user is admin or not.

Full Stack Web development


if else if ladder:

JavaScript also supports the use of else if statement, which allows you to chain multiple conditions together
also called if else ladder. 

The syntax for an if else if ladder is as follows:

Note 1: You can chain as many else-if statements as you want.

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:

Let’s write a program to check if the given number is odd or even

Full Stack Web development

You might also like