Conditional Branching - JavaScript Interview
JavaScript Fundamentals: Conditional Branching
What is an if statement in JavaScript?
View Answer:
Interview Response: An if statement is a conditional statement that checks a specified condition and executes a block of code if the condition is true. If the condition is false, the code inside the if statement is ignored.
Code Example:
let year = prompt('In which year was ECMAScript-2015 published?', '');
if (year == 2015) console.log('You are right!'); // You are right!
Can you execute more than one statement in an if statement?
View Answer:
Interview Response: Yes, but they must be encased in curly brackets . Even if just one sentence has to run, this improves readability and is encouraged.
Code Example:
if (year == 2015) {
console.log("That's correct!");
console.log("You're so smart!");
}
What is conditional branching in JavaScript?
View Answer:
Interview Response: Conditional branching is a programming concept that allows code execution to take different paths based on specified conditions. It enables the execution of specific code blocks depending on whether certain conditions are true or false. Conditional branching in JavaScript involves using statements like 'if', 'else', and 'else if' to execute specific code blocks depending on whether certain conditions are met, allowing for dynamic behavior.
Code Example:
let temperature = 75;
if (temperature > 90) {
console.log("It's hot outside!");
} else if (temperature > 80) {
console.log("It's warm outside.");
} else {
console.log("It's cool outside.");
}
What type of conversion does the JavaScript “if” statement use?
View Answer:
Interview Response: The JavaScript "if" statement uses implicit type coercion, converting the condition to a boolean value. This is done through the Abstract Equality Comparison algorithm, often called "loose" comparison.
Can you pre-evaluate a condition for use in an if statement?
View Answer:
Interview Response: Yes, you can pre-evaluate a condition by assigning its result to a variable and then using that variable as the condition in the if statement.
Code Example:
let cond = year == 2015; // equality evaluates to true or false
if (cond) {
console.log('Hello, World'); // returns Hello, World
}
Is there a way to handle false conditions in an if statement?
View Answer:
Interview Response: Yes, We can use an if-else statement to handle false conditions.
Technical Response: Yes, the “if” statement may contain an optional “else” block. It executes when the condition is false.
Code Example:
let year = prompt('In which year was the ECMAScript-2015 published?', '');
if (year == 2015) {
console.log('You guessed it right!');
} else {
console.log('How can you be so wrong?'); // any value except 2015
}
If you want to test a variety of circumstances. What kind of conditional statement can you use with an if statement?
View Answer:
Interview Response: To test various circumstances, you can use "else if" clauses along with the "if" statement, creating a chain of conditions to check before executing a specific code block.
Code Example:
let year = prompt('In which year was the ECMAScript-2015 published?', '');
if (year < 2015) {
console.log('Too early...');
} else if (year > 2015) {
console.log('Too late');
} else {
console.log('Exactly!');
}
Can you assign a variable depending on a condition in an if statement?
View Answer:
Interview Response: Yes, in JavaScript you can assign a variable depending on a condition in an if statement.
Code Example:
let accessAllowed;
let age = prompt('How old are you?', '');
if (age > 18) {
accessAllowed = true;
} else {
accessAllowed = false;
}
console.log(accessAllowed);
Is there a shorthand version of the if statement that you can use in JavaScript?
View Answer:
Interview Response: JavaScript offers a shorthand version called the ternary operator (condition ? trueExpression : falseExpression) for simple if-else statements, allowing for concise inline conditional evaluations.
Code Example:
// the comparison operator "age > 18" executes first anyway
// (no need to wrap it into parentheses)
let accessAllowed = age > 18 ? true : false;
Note: Because the comparison itself gives true/false, you may eliminate using the question mark operator in the above example:
// the same
let accessAllowed = age > 18;
What is the syntax of an if statement in JavaScript?
View Answer:
Interview Response: In JavaScript, the syntax for an if statement starts with the keyword 'if', followed by a condition in parentheses. Then, code blocks for true and optional false conditions are enclosed in curly braces.
What is the else keyword used for in JavaScript?
View Answer:
Interview Response: The "else" keyword is used in conjunction with an "if" statement to provide an alternative code block to execute when the condition of the "if" statement is false.
What is a switch statement in JavaScript?
View Answer:
Interview Response: A switch statement is a control structure used to execute different code blocks based on the value of a specified expression, simplifying multiple if-else conditions.
Code Example:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
default:
dayName = "Unknown";
}
console.log("Today is " + dayName);
In this example, the value of the day
variable is evaluated in the switch statement. Based on the value, the code execution "falls through" to the matching case, assigning the corresponding dayName
. If no matching case is found, the code falls back to the default case. Finally, the assigned dayName
is logged to the console.
What is the syntax of a switch statement in JavaScript?
View Answer:
Interview Response: The switch statement syntax includes the 'switch' keyword, followed by an expression that is compared to values in each "case" block. When a match is found, the code in the corresponding block is executed. Optionally, a 'default' clause is added.
What is a ternary operator in JavaScript?
View Answer:
Interview Response: A ternary operator in JavaScript is a concise, shorthand way to write simple if-else statements.
Code Example:
let age = 20;
let message = (age >= 18) ? "You are an adult" : "You are a minor";
console.log(message); // logs "You are an adult"
Can multiple conditions be combined in a single if statement?
View Answer:
Interview Response: Yes, multiple conditions can be combined in a single if statement using logical operators such as '&&' (AND), '||' (OR), and '!' (NOT) to create complex conditionals.
Code Example:
let temperature = 25;
let time = "morning";
if (temperature > 20 && time === "morning") {
console.log("It's a warm morning!");
}
In this example, the if statement combines two conditions using the logical AND operator (&&
). It checks if the temperature is greater than 20 AND if the time is "morning". If both conditions evaluate to true, it executes the code block inside the if statement, which logs "It's a warm morning!" to the console.
How many conditional branching methods does JavaScript have?
View Answer:
Interview Response: JavaScript has three primary conditional branching methods: if-else statements, switch statements, and the ternary operator, each serving different use cases and levels of complexity.