JavaScript Conditional Statements
What are conditional statements in JavaScript?
Conditional statements in JavaScript are used to perform different actions based on different conditions. They allow you to execute code blocks based on whether certain conditions evaluate to true or false.
What is the syntax of an if statement in JavaScript?
The syntax of an if statement is as follows:
if (condition) {
// Code to execute if condition is true
}
Can you provide an example of an if statement?
let age = 18;
if (age >= 18) {
console.log('You are an adult.');
}
What is the purpose of the else statement?
The else statement executes a block of code if the condition in the if statement is false.
if (age >= 18) {
console.log('You are an adult.');
} else {
console.log('You are a minor.');
}
What is an else if statement?
The else if statement allows you to test multiple conditions. If the first condition is false, the next condition will be checked.
let score = 85;
if (score >= 90) {
console.log('Grade: A');
} else if (score >= 80) {
console.log('Grade: B');
} else if (score >= 70) {
console.log('Grade: C');
} else {
console.log('Grade: F');
}
What is the switch statement in JavaScript?
The switch statement evaluates an expression and executes code blocks based on matching cases. It is often used as an alternative to multiple if statements when checking the same variable.
let fruit = 'apple';
switch (fruit) {
case 'banana':
console.log('Banana is yellow.');
break;
case 'apple':
console.log('Apple is red.');
break;
default:
console.log('Unknown fruit.');
}
What is the significance of the break statement in a switch case?
The break statement is used to exit the switch block after a case has been executed. Without it, the program will continue executing the subsequent cases (fall-through behavior).
Can you explain the use of the default case in a switch statement?
The default case in a switch statement is executed when none of the specified cases match the expression. It acts like the else statement in an if-else chain.
switch (fruit) {
case 'banana':
console.log('Banana is yellow.');
break;
case 'apple':
console.log('Apple is red.');
break;
default:
console.log('Unknown fruit.'); // Executed if no case matches
}
What is the ternary operator in JavaScript?
The ternary operator is a shorthand for the if-else statement. It takes three operands: a condition, the result if true, and the result if false.
let age = 18;
let status = (age >= 18) ? 'Adult' : 'Minor';
console.log(status); // Adult
How can you use logical operators in conditional statements?
You can use logical operators (&&, ||, !) to combine multiple conditions in conditional statements.
let age = 20;
let hasPermission = true;
if (age >= 18 && hasPermission) {
console.log('Access granted.');
} else {
console.log('Access denied.');
}
What is short-circuit evaluation in JavaScript?
Short-circuit evaluation occurs with logical operators where the second operand is evaluated only if necessary. For example, in an AND operation, if the first operand is false, the second operand is not evaluated.
let x = false;
let y = (x && someFunction()); // someFunction() is not called because x is false
How can you check if a variable is truthy or falsy in JavaScript?
In JavaScript, values are considered truthy or falsy based on their inherent Boolean value. Falsy values include 0, "", false, undefined, null, and NaN. All other values are truthy.
let value = 0;
if (value) {
console.log('This is truthy');
} else {
console.log('This is falsy'); // This will be logged
}
What is the purpose of the conditional operator?
The conditional operator (also known as the ternary operator) provides a shorthand way to write simple if-else statements, making the code more concise.
const isAdult = (age >= 18) ? 'Yes' : 'No';
console.log(isAdult); // Yes