Skip to content

Quiz: Predict the Output (Basics)

beginner12 min read

Time to Put It All Together

You've worked through variables, operators, functions, arrays, objects, destructuring, strings, error handling, and modules. Now let's see how well those concepts actually stuck.

This isn't about memorizing syntax. It's about having a mental model of how JavaScript evaluates code, step by step. Each question below is something you could genuinely encounter in real code. No trick questions for the sake of being tricky — every one tests a concept that matters.

How to use this quiz: Read each snippet, trace the execution in your head (or on paper), pick your answer, then read the explanation carefully — even if you got it right. The explanations connect back to the "why" behind the behavior.

Mental Model
Key Rules
  1. 1Trace line by line, never skip ahead or assume
  2. 2Pay attention to var vs let vs const — they hoist differently
  3. 3When you see +, ask: are both operands numbers? If not, expect string concatenation
  4. 4Function declarations are hoisted entirely, function expressions are not
  5. 5Objects and arrays are references — copying a variable copies the pointer, not the data

Question 1: The Warm-Up

let x = 10;
let y = "5";
console.log(x + y);
console.log(x - y);
Quiz
What does this code output?

Question 2: Hoisting Surprise

console.log(a);
console.log(b);
var a = 5;
let b = 10;
Quiz
What happens when this code runs?

Question 3: Function Hoisting

console.log(greet());
console.log(hello());

function greet() {
  return "Hi!";
}

var hello = function () {
  return "Hello!";
};
Quiz
What does this code output?

Question 4: Scope Boundaries

for (var i = 0; i < 3; i++) {
  // loop body
}
console.log(i);

for (let j = 0; j < 3; j++) {
  // loop body
}
console.log(j);
Quiz
What does this code output?

Checkpoint: How's It Going?

Four questions down. If you got at least 3 right, your fundamentals are solid. If hoisting tripped you up, that's completely normal — it trips up experienced developers too. The key insight is: declarations are hoisted, assignments are not, and let/const have a temporal dead zone that var does not.


Question 5: Array Methods and Return Values

const nums = [1, 2, 3, 4, 5];
const result = nums.filter(n => n > 2).map(n => n * 10);
console.log(result);
console.log(nums);
Quiz
What does this code output?

Question 6: Object References

const person = { name: "Alice", age: 25 };
const copy = person;
copy.name = "Bob";
console.log(person.name);
console.log(copy.name);
Quiz
What does this code output?

Question 7: Destructuring with Defaults

const config = { theme: "dark", lang: undefined };
const { theme, lang = "en", debug = false } = config;
console.log(theme);
console.log(lang);
console.log(debug);
Quiz
What does this code output?

Question 8: Template Literal Expressions

const a = 10;
const b = 20;
console.log(`Sum: ${a + b}`);
console.log("Sum: " + a + b);
Quiz
What does this code output?

Checkpoint: Halfway There

Eight questions in. If you nailed 6 or more, you're building strong instincts. The next four questions ramp up the difficulty. They combine multiple concepts — exactly the kind of code you will see in production.


Question 9: The typeof Quirk

console.log(typeof null);
console.log(typeof undefined);
console.log(typeof NaN);
Quiz
What are the three results?

Question 10: Error Handling Flow

function divide(a, b) {
  try {
    if (b === 0) throw new Error("Cannot divide by zero");
    return a / b;
  } catch (err) {
    console.log(err.message);
    return -1;
  } finally {
    console.log("done");
  }
}

console.log(divide(10, 0));
Quiz
What does this code output?

Question 11: Spread and Shallow Copy

const original = { a: 1, b: { c: 2 } };
const clone = { ...original };
clone.a = 99;
clone.b.c = 99;
console.log(original.a);
console.log(original.b.c);
Quiz
What does this code output?

Question 12: Putting It All Together

var count = 0;

function increment() {
  count++;
  return count;
}

const results = [increment(), increment(), increment()];
const doubled = results.map(n => n * 2);
const [first, ...rest] = doubled;

console.log(first);
console.log(rest);
console.log(count);
Quiz
What does this code output?

Scoring Guide

ScoreAssessment
11-12Excellent. Your JavaScript fundamentals are rock solid. You are ready for the deep dive module.
8-10Strong foundation with a few gaps. Re-read the explanations for the ones you missed — those are the exact areas to strengthen.
5-7You have the basics but some concepts need more practice. Revisit the specific topics that tripped you up before moving on.
0-4Go back through the fundamentals module. Focus on tracing code line by line instead of guessing. The mental model section at the top is your friend.

No matter your score, remember: understanding the why behind the behavior matters more than getting the right answer by instinct. Every question you missed is a concept you just learned more deeply by reading the explanation.