Q1. What is an if statement in JavaScript?
An if statement is a conditional statement that executes a block of code only if a specified condition is true.
It helps JavaScript programs make decisions and run code based on conditions.
Example
It helps JavaScript programs make decisions and run code based on conditions.
Example
let age = 20;
if (age >= 18) { console.log("You are eligible to vote");}Q2. What is an If-Else If Ladder?
An if-else if ladder is used to check multiple conditions one by one.
If the first condition is false, JavaScript checks the next condition.
Example
If the first condition is false, JavaScript checks the next condition.
Example
let marks = 85;
if (marks >= 90) { console.log("Grade A");} else if (marks >= 75) { console.log("Grade B");} else { console.log("Grade C");}Q3. Can we use multiple conditions inside an If Statement?
Yes, multiple conditions can be used using logical operators like && (AND) and || (OR).
Example
Example
let age = 25;let hasLicense = true;
if (age >= 18 && hasLicense) { console.log("You can drive");}Q4. What are Truthy and Falsy values in If Statements?
JavaScript treats some values as true and some as false when used in conditions.
All other values are considered truthy.
- false
- 0
- "" (empty string)
- null
- undefined
- NaN
Q5. Can If Statements be nested?
Yes, an if statement can be placed inside another if statement. This is called nesting.
Example
Example
let age = 20;let isStudent = true;
if (age >= 18) { if (isStudent) { console.log("Eligible student"); }}