JavaScript Conditional Statements Interview Questions and Answers
Practice reasoning about conditions, branching, and choosing between if/else and other decision structures.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
The
ternary
operator (? :) is JavaScript's only operator that takes three operands. It's a compact if-else on a single line: condition ? valueIfTrue : valueIfFalse. Great for simple assignments like const status = age >= 18 ? "adult" : "minor". But resist the urge to nest them - a ternary inside a ternary inside a ternary is a code review nightmare. If the logic is complex, just use a regular if-else block.
A control structure is how you tell your program to make decisions and repeat work instead of just running line by line from top to bottom. Without them, your code would be a straight line with no branches. The three main types are: sequential (code runs in order, the default), selection (if/else, switch - pick a path based on a condition), and iteration (for, while - repeat a block until a condition changes). Interviewers like to hear you name all three.
JavaScript evaluates the if condition and converts its result to a boolean. It runs the if block when the result is truthy; otherwise, it runs the optional else block. An else-if chain checks each condition in order and runs only the first matching branch.
const score = 82;
if (score >= 70) {
console.log('Pass');
} else {
console.log('Try again');
}It converts the condition to a Boolean using truthiness; values such as 0, an empty string, null, and undefined are falsy.
Conditions are checked from top to bottom, and only the first truthy branch runs; order more specific conditions before broader ones.
A switch uses strict equality semantics for case matching, so values of different types do not match through coercion.
Use it for a concise choice between two values. Prefer if/else when nesting or side effects would make the expression difficult to read.
The nullish coalescing operator defaults only for null or undefined, while || also replaces valid falsy values such as 0, false, and an empty string.
A guard clause handles an invalid or exceptional condition early and returns, reducing unnecessary nesting in the normal path.
Short-circuit operators may skip their right operand, making side effects conditional and less obvious. Use explicit branches when control flow or mutation matters.
A legitimate zero is falsy, so value || fallback replaces it. Use value ?? fallback when only null and undefined mean missing.
Case clauses share the switch block lexical scope, so repeated let or const declarations can conflict. Wrap each case body in braces to create separate scopes.
External or evolving values may not match any known case. A default branch can reject or safely handle unsupported input instead of silently doing nothing.
Check invalid bounds before narrower valid ranges, then make ranges mutually exclusive. This prevents an early broad condition from capturing values intended for a later case.
JavaScript Practical and Coding Interview Questions
Problem: Create classifyScore(score): return perfect for 100, pass for scores at least 60 and below 100, fail otherwise within 0?100, and invalid outside that range. Log results for 100, 75, 40, and -1.
function classifyScore(score) {
if (!Number.isFinite(score) || score < 0 || score > 100) return 'invalid';
if (score === 100) return 'perfect';
if (score >= 60) return 'pass';
return 'fail';
}
[100, 75, 40, -1].forEach(score => {
console.log(score, classifyScore(score));
});