JavaScript Statements Interview Questions and Answers
Prepare to discuss statements, code blocks, semicolons, and how JavaScript organizes executable instructions.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- What is a statement in programming?
- What is a code block?
- What is a statement in JavaScript?
- Calculate and print a discounted price with one statement.
- Calculate and print a price with tax using separate statements.
- Why can mutating an array while iterating it be error-prone?
- How does an `if...else` statement choose which block to run?
Theoretical JavaScript Interview Questions
A statement is one complete instruction - declare a variable, call a function, run a loop, branch with an if. Think of a program as a series of statements the engine executes top-to-bottom. Statements differ from expressions in that they perform an action rather than produce a value.
A code block is a set of statements wrapped in curly braces {} that are treated as a single unit. It's how JavaScript knows which statements belong to a function, loop, or if-branch. Importantly, let and const declared inside a block are scoped to that block - they don't leak out, which is a key reason to prefer them over var.
A statement is an instruction or control structure that performs an action, such as a declaration, loop, or conditional. Expressions produce values and can be used within statements.
JavaScript evaluates the condition using truthiness and runs the `if` block when it is truthy; otherwise it runs the `else` block if present. An `else if` chain checks later conditions only when earlier conditions were false.
Execution continues into the next case when a matched case does not end with a control transfer such as `break`, `return`, or `throw`. Fall-through can be intentional, but otherwise add a `break` to avoid running later case bodies.
`break` exits the nearest loop or switch, while `continue` skips the remainder of the current loop iteration and proceeds to the next iteration. Labels can target an enclosing statement when nested control flow requires it.
A `finally` block normally runs after the `try` and optional `catch` complete, including when control returns or throws. Abrupt termination of the execution environment is outside that guarantee.
A block groups multiple statements into one body and makes control flow easier to read. It also creates a lexical scope for `let` and `const` declarations inside it.
`switch` compares the discriminant with each case expression using strict equality semantics. It does not perform the coercion that loose equality would allow.
A control transfer from `finally`, such as `return` or `throw`, overrides a pending return or exception from `try` or `catch`. Returning from `finally` is therefore usually avoided because it can hide earlier outcomes.
A labeled `break` can exit a specifically labeled enclosing loop or block, which is useful when nested loops need a direct exit. Labels are valid only for breakable statements and should be used sparingly for clarity.
A `do...while` loop executes its body once before checking the condition, so it always runs at least once. A `while` loop checks first and may run zero times.
Adding or removing elements can shift indices or change the iteration range, causing elements to be skipped or processed unexpectedly. Prefer creating a new result or iterate in a direction that safely accounts for the mutation.
JavaScript Practical and Coding Interview Questions
Problem: Use one JavaScript statement to calculate a 15% discount on 120 and print the final price.
console.log(120 * 0.85);Problem: Store a subtotal, calculate 8% tax, calculate the total, and print the total using separate statements.
const subtotal = 50;
const tax = subtotal * 0.08;
const total = subtotal + tax;
console.log(total);