Practice · Code Reading

What does this print?

Read the code, pick what it prints, then see why. This is the skill behind the new code comprehension rounds: reading code you didn't write, and knowing what it really does. Every program here has been run, and the answers are what it actually prints.

35 JavaScript drills15 Python drills →
  1. 1. Use it before you write it

    Lesson 3
    hoisting.js
    sayHi();console.log(score);var score = 50;function sayHi() {  console.log("Hi there!");}
    What does this print?
  2. 2. Same name, which city?

    Lesson 4
    cities.js
    var city = "Delhi";function showHome() {  var city = "Pune";}showHome();console.log(city);
    What does this print?
  3. 3. undefined vs not defined

    Lesson 6
    marks.js
    console.log(marks);var marks = 95;console.log(grade);
    What does this print?
  4. 4. Whose variable is it?

    Lesson 7
    kitchen.js
    function kitchen() {  console.log(owner);}var owner = "Asha";kitchen();
    What does this print?
  5. 5. var vs let, before the line

    Lesson 8
    shop.js
    console.log(count);console.log(price);var count = 3;let price = 99;
    What does this print?
  6. 6. What leaks out of the braces?

    Lesson 9
    block.js
    {  var coins = 10;  let lives = 3;}console.log(coins);console.log(lives);
    What does this print?
  7. 7. It remembers

    Lesson 10
    greeter.js
    function makeGreeter() {  var name = "Riya";  return function () {    console.log("Hi " + name);  };}var greet = makeGreeter();greet();
    makeGreeter has already finished. What does greet() print?
  8. 8. The classic interview loop

    Lesson 11
    count.js
    for (var i = 1; i <= 3; i++) {  setTimeout(function () {    console.log(i);  }, i * 10);}
    What does this print?
  9. 9. The same loop, with let

    Lesson 11
    count-let.js
    for (let i = 1; i <= 3; i++) {  setTimeout(function () {    console.log(i);  }, i * 10);}
    Now with let. What does it print?
  10. 10. Statement vs expression

    Lesson 13
    difference.js
    greet();wave();function greet() {  console.log("Hello!");}var wave = function () {  console.log("Wave!");};
    What does this print?
  11. 11. Who prints last?

    Lesson 15
    timer.js
    console.log("start");setTimeout(function cb() {  console.log("timer");}, 50);console.log("end");
    What does this print, in order?
  12. 12. Zero milliseconds, still last

    Lesson 17
    zero.js
    console.log("Start");setTimeout(() => console.log("Callback"), 0);console.log("End");
    What does this print, in order?
  13. 13. map, filter, reduce

    Lesson 19
    prices.js
    const prices = [120, 80, 250];console.log(prices.map((p) => p + 20));console.log(prices.filter((p) => p < 100));console.log(prices.reduce((sum, p) => sum + p, 0));
    What does this print?
  14. 14. What's inside the promise?

    Lesson 21
    inside.js
    const booking = new Promise((resolve) => {  setTimeout(() => resolve("BK-101"), 100);});console.log(booking);
    What does console.log print?
  15. 15. Resolved already, printed later

    Lesson 22
    book.js
    new Promise((resolve) => {  resolve("BK-101");}).then((id) => console.log(id));console.log("booking...");
    What does this print, in order?
  16. 16. What does async return?

    Lesson 23
    async.js
    async function getCourse() {  return "Inside JavaScript";}console.log(getCourse());
    What does console.log print?
  17. 17. One of them fails

    Lesson 25
    all.js
    const details = new Promise((resolve) => setTimeout(() => resolve("details"), 200));const showtimes = new Promise((_, reject) => setTimeout(() => reject(new Error("Showtimes failed")), 100));Promise.all([details, showtimes])  .then((r) => console.log(r))  .catch((e) => console.log(e.message));
    What does this print?
  18. 18. Who wins the race?

    Lesson 26
    race.js
    const details = new Promise((resolve) => setTimeout(() => resolve("details"), 200));const showtimes = new Promise((resolve) => setTimeout(() => resolve("showtimes"), 100));Promise.race([details, showtimes]).then((v) => console.log(v));
    What does this print?
  19. 19. Who is this?

    Lesson 27
    show.js
    "use strict";function show() {  console.log(this);}show();
    In strict mode, what does this print?
  20. 20. Where did this go?

    Lesson 28
    movie.js
    "use strict";const movie = {  title: "Inception",  show() {    console.log(this.title);  },};movie.show();const show = movie.show;show();
    What does this print?
  21. 21. The local one, too early

    Lesson 3
    shadow.js
    var x = 1;function show() {  console.log(x);  var x = 2;}show();
    What does this print?
  22. 22. Two counters

    Lesson 10
    counter.js
    function makeCounter() {  let count = 0;  return () => ++count;}const a = makeCounter();const b = makeCounter();a();console.log(a(), b());
    What does this print?
  23. 23. var in a loop, fixed

    Lesson 11
    loop.js
    for (var i = 0; i < 3; i++) {  (function (j) {    setTimeout(() => console.log(j), 0);  })(i);}
    What does this print?
  24. 24. A function that returns a function

    Lesson 13
    curry.js
    const add = (a) => (b) => a + b;const addTen = add(10);console.log(addTen(5), add(2)(3));
    What does this print?
  25. 25. map with parseInt

    Lesson 19
    parse.js
    const nums = ["1", "2", "3"].map(parseInt);console.log(nums);
    What does this print?
  26. 26. Reduce an empty array

    Lesson 19
    total.js
    const total = [].reduce((sum, x) => sum + x);console.log(total);
    What happens?
  27. 27. After the catch

    Lesson 21
    chain.js
    Promise.resolve(1)  .then((x) => x + 1)  .then(() => { throw new Error("oops"); })  .catch(() => 5)  .then((x) => console.log(x));
    What does this print?
  28. 28. An async function that throws

    Lesson 23
    load.js
    async function load() {  throw new Error("boom");}load().catch((e) => console.log("caught", e.message));console.log("after");
    What does this print?
  29. 29. Where await pauses

    Lesson 23
    run.js
    async function run() {  console.log("A");  await null;  console.log("B");}run();console.log("C");
    In what order are the letters printed?
  30. 30. Wait for everyone

    Lesson 25
    settled.js
    Promise.allSettled([  Promise.resolve(1),  Promise.reject(new Error("no")),]).then((r) => console.log(r.map((x) => x.status).join(",")));
    What does this print?
  31. 31. The first success

    Lesson 26
    any.js
    Promise.any([  Promise.reject(new Error("a")),  new Promise((ok) => setTimeout(() => ok("slow"), 50)),  Promise.resolve("fast"),]).then((v) => console.log(v));
    What does this print?
  32. 32. bind, then bind again

    Lesson 28
    bind.js
    function getX() {  return this.x;}const once = getX.bind({ x: 1 });const twice = once.bind({ x: 2 });console.log(twice());
    What does this print?
  33. 33. An arrow function as a method

    Lesson 28
    user.js
    const user = {  city: "Pune",  where: () => console.log(this.city),  whereNow() { console.log(this.city); },};user.where();user.whereNow();
    What does this print?
  34. 34. Loose equality

    equal.js
    console.log([] == false, null == 0, null >= 0);
    What does this print?
  35. 35. Two names, one object

    score.js
    const a = { score: 1 };const b = a;b.score = 2;const c = { ...a };c.score = 3;console.log(a.score, b.score, c.score);
    What does this print?