JavaScript, inside out · Chapter 51 of 51

Output prediction

By the end of this chapter you can read a short snippet and say exactly what it logs, by naming the rule behind every line. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The interview
  • 6 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

The last round. Dev closes the editor and turns his laptop around: six short snippets and no console. "What does each one print? Talk me through it."

You have met every one of these rules, from chapter 7 to chapter 41. What changes in an interview is that you cannot run the code, and a guess that is right for the wrong reason falls apart at the follow-up question. The way through is to name the rule for each line out loud. This chapter puts the rules side by side, with fresh examples; the six snippets in exercise.js are yours to work out.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: hoisting, what is ready before its line runs

    Export hoisting, an array of every value snippet 1 (hoisting) logs, in the order they are logged. log(x) records one value, so write each value as the value itself: strings as strings, numbers as numbers, booleans as booleans, and undefined or NaN as undefined or NaN.

    The check runs the same snippet and compares each value with Object.is (so NaN matches NaN, and 40 does not match '40').

  2. Task 2 · interview question

    Interview: loops, closures in loops and timers

    Export loops, an array of every value snippet 2 (closures in loops) logs, in the order they are logged. log(x) records one value, so write each value as the value itself: strings as strings, numbers as numbers, booleans as booleans, and undefined or NaN as undefined or NaN.

    The check runs the same snippet and compares each value with Object.is (so NaN matches NaN, and 40 does not match '40').

  3. Task 3 · interview question

    Interview: thisKeyword, this five ways

    Export thisKeyword, an array of every value snippet 3 (this) logs, in the order they are logged. log(x) records one value, so write each value as the value itself: strings as strings, numbers as numbers, booleans as booleans, and undefined or NaN as undefined or NaN.

    The check runs the same snippet and compares each value with Object.is (so NaN matches NaN, and 40 does not match '40').

  4. Task 4 · interview question

    Interview: coercion, plus, minus and ==

    Export coercion, an array of every value snippet 4 (coercion) logs, in the order they are logged. log(x) records one value, so write each value as the value itself: strings as strings, numbers as numbers, booleans as booleans, and undefined or NaN as undefined or NaN.

    The check runs the same snippet and compares each value with Object.is (so NaN matches NaN, and 40 does not match '40').

  5. Task 5 · interview question

    Interview: eventLoop, microtasks and a timer

    Export eventLoop, an array of every value snippet 5 (the event loop) logs, in the order they are logged. log(x) records one value, so write each value as the value itself: strings as strings, numbers as numbers, booleans as booleans, and undefined or NaN as undefined or NaN.

    The check runs the same snippet and compares each value with Object.is (so NaN matches NaN, and 40 does not match '40').

  6. Task 6 · interview question

    Interview: promises, executor, resolve twice and catch

    Export promises, an array of every value snippet 6 (promises) logs, in the order they are logged. log(x) records one value, so write each value as the value itself: strings as strings, numbers as numbers, booleans as booleans, and undefined or NaN as undefined or NaN.

    The check runs the same snippet and compares each value with Object.is (so NaN matches NaN, and 40 does not match '40').

What you will learn

  • Hoisting and closures: what exists before its line runs, and which variable a function remembers.
  • this: decided by how a function is called.
  • Coercion: what +, - and == turn values into.
  • The event loop and promises: which queue a callback waits in, and how a chain carries on.

More in The interview

Build "Output prediction" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.