JavaScript Interview Questions that made me think — Do I really know JavaScript? — Part 1

javascript tricky interview questions

Photo by Rahul Mishra on Unsplash

The best part of JavaScript which always amaze me is that there is always something new happening around it. And no matter how much you know about it, you will always get to learn new things about it.

I’ve collected these questions over a period of quite long time. In most of the questions I really had no idea about what the output would be, until I tried them by myself.

So here I am documenting them so that other people can make use of it to learn new concepts:

.

.

.

Output:

21
{name: “Lydia”} // age won’t be included. Because property defined with defineProperty are non enumerable by default.

.

.

.

Output:

false // delete operator only deletes a key in object
true // when we don't use any declaration before any variable, it will be treated as a global variable, and will be added as deletable entity in window object.
undefined

.

.

.

Output:

[ { name: "Noren Red"} ] Initially I thought it will log [ null ] because we have initialised person with nullBut in reality, we are only setting new reference to person variable. Previous reference will be used in members arrayIn Short, { name: "Noren Red"} lives in some memory space whose address is X201and this is how referencing is workinglet person = X201
const members = [ X201 ]
person = null

.

.

.

Output

Silver Surfer Because when we return a property, the value of the property is equal to the returned value, not the value set in the constructor function.

Output:


With the padStart method, we can add padding to the beginning of a string.

The value passed to this method is the total length of the string together with the padding.
The string "Silver Surfer" has a length of 13. name.padStart(14) inserts 1 space at the start of the string, because 13 + 1 is 13. If the argument passed to the padStart method is smaller than the length of the array, no padding will be added.

.

.

.

Output

777If we pass string and number combination to parseInt, what parseInt does is, it check at which position wrong datatype is getting started, if value before the wrong datatype is a valid number, it will return the valid number.

.

.

.

Output:

1 2 and undefined 3 and undefined 4 
If we don't pass initial value, then by default x will be first value of array, and y will be second value of array.

.

.

.

Output:

one - ["", " is ", " years old"]
two - Thor
three - 1000
one - ["hey there, are you amazed"]
two - undefined
three - undefined
If we use tagged template literals, the value of the first argument is always an array of the string values.The remaining arguments get the values of the passed expressions!

.

.

.

Output:

1
undefined
2

.

.

.

Output

function // Classes in JS are functions under the hood

--

--