JavaScript: Predict the Output Questions vol. 3

Elizabeth Westphal
3 min readMar 29, 2022

This is the third entry in my series where I try to trick you with some predict the output questions. These types of questions come up frequently in interviews and they may throw you for a loop if you’ve only been practicing algorithms questions on LeetCode. Below are questions, answers, and explanations on the quirks of the JavaScript language. These problems are meant to help you brush up on your knowledge or prep for an upcoming interview. I recommend trying the question on your own before scrolling to the answer.

Problem 1:

Predict the output of the code snippet below:


“155cm” is logged to the console. Let’s take a look at what’s happening. On line 6 when the constructor function is called, the value of 165cm is assigned to the property of height. Even though 165 is assigned, the return statement is ultimately what dictates the returned value when height is accessed. Think of the return statement as an override, as it is not actually changing the value of height, it is simply reporting the value the programmer tells it to.

Problem 2:

What will be logged to the console after running the attached code snippet?


An error will be thrown. An imported module is read-only, so we cannot modify tracker.js from script.js. Only the exporting module, in this case tracker.js can modify this value.

Problem 3:

Predict the output of the following code:


‘x’ is logged to the console. Not sure how let happened? Let’s think of another example:

In this case ‘xy’ is logged to the console. This is a concept called destructuring. When we assign an array to an array in javascript what we are actually doing is assigning the first value of the second array to the variable named after the first element of the first array, and so on for the following values. In this case we are assigning the value ‘x’ to a variable named a, and the value ‘y’ to a variable named b. The first example only outputs ‘x’ because there was only one value in the first array to which a value of the second array could be assigned.

Thanks for reading and please subscribe for more JavaScript lessons, tutorials, and interview prep!

Recommended from Medium


See more recommendations