JavaScript Conditional Statements

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:01:00

Control Flow:

Control flow refers to the order in which instructions are executed in a program. It determines the path the program takes based on conditions and decisions.

Default Control Flow Order:

In JavaScript, code is executed sequentially from top to bottom by default. This means that statements are processed in the order they appear in the code.

Control Structures and Conditionals:

Control structures like if statements, switch statements, and loops enable you to make decisions and control the flow of your program based on certain conditions.

if Statement:

The 'if' statement is used to execute a block of code if a specified condition is 'true'.

Example:

let temperature = 28;
if (temperature > 30) {
    console.log("It's a hot day.");
} 

else Statement:

The 'else' statement is used alongside an 'if' statement. It executes a block of code when the condition of the 'if' statement is 'false'.

Example:

let weather = "rainy";
if (weather === "sunny") {
    console.log("It's sunny!");
} else {
    console.log("It's not sunny.");
}

switch Statement:

The 'switch' statement allows you to evaluate an expression against multiple possible case values and execute corresponding code blocks.

Example:

let day = "Monday";
switch (day) {
    case "Monday":
        console.log("Start of the week");
        break;
    case "Friday":
        console.log("End of the week");
        break;
    default:
        console.log("Somewhere in between");
}

Truthy and Falsy Values:

In JavaScript, values that are considered "truthy" evaluate to 'true' in boolean contexts, while "falsy" values evaluate to 'false'.

Example:

let username = ""; // Falsy
if (username) {
    console.log("Username provided.");
} else {
    console.log("Username not provided.");
} 

else if Clause:

The 'else if' clause is used to specify an additional condition to check if the preceding 'if' or 'else if' conditions are false.

Example:

let grade = 85;
if (grade >= 90) {
    console.log("A");
} else if (grade >= 80) {
    console.log("B");
} else if (grade >= 70) {
    console.log("C");
} else {
    console.log("F");
} 
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this