02 JS - Conditional Operator

02 JS - Conditional Operator

ยท

2 min read

Q1. Expression v/s Statement ?

  1. Expression:

    • An expression is a piece of code that produces a value. It can be a simple value like a number or a string, or it can be a more complex combination of variables, operators, and function calls.

    • Examples of expressions:

        5 + 3         // Evaluates to 8
        variableName  // The value stored in the variable
        myFunction()  // The return value of a function call
      
  2. Statement: - (multiple line mein value mile)

    • A statement is a complete line of code that performs an action. Unlike expressions, statements don't necessarily produce a value. They are typically used to control the flow of a program or to perform some operation.

    • Examples of statements:

        let x = 10;                  // Variable declaration statement
        if (x > 5) {                  // If statement
          console.log("x is greater than 5");
        } else {                      // Else statement
          console.log("x is not greater than 5");
        }
      

Note: In JavaScript, some statements, like the if statement and the return statement, can include expressions, but the statement itself doesn't necessarily result in a value.

Q2. Conditional Statements:

Making Decisions in Code

Imagine you are instructing a computer, and sometimes you want it to do different things based on certain conditions. This is where conditional statements come into play.

conditional statements help your instructions adapt to different situations. If something is true, do one thing; if it's not true, do another. It's like giving your computer a set of rules to follow based on what's happening.

ย