10 Tricky JavaScript Snippets for Programming Interviews

Prepare yourself for that desired job

Job interviews are not entirely predictable, but we often still see commonly asked tricky questions. Let’s take a look at ten of them.

1. Shallow Copy

Output

original: {
father: ‘MD’,
age: 26,
email: ‘shoaib.mehedi@gmail.com’
}
copy: {
father: ‘MD’,
age: 26,
email: ‘shoaib.mehedi@gmail.com’
}

Why?

Object.assign and the spread operator both do a shallow copy.That means we copied the first-level object.

The code we used before can be written like this with Object.assign:

2. JavaScript Hoisting

Predict the output of the code below:

Output

undefined
26

Why?

The output of the code snippets is not shoaib and 20. The result is undefined and 26 because of hoisting in JavaScript.

The code above will be converted like so:

3. Value Assignment

Output

numTwo false
numOne true

Similarly, we can write it like so:

function assign() {
  var numOne= (numTwo = 10);
}

Why?

The assignment operator has right-to-left associativity, which means it will be evaluated from right to left. That’s why numTwo will be assigned the value 10, and then it’s assigned to numOne.

4. JavaScript this

Output

outer function:  this.name = shoaib
outer function:  self.name = shoaib
inner function:  this.name = undefined
inner function:  self.name = shoaib

Why?

The outer functions this and self refer to obj, so both have proper access to the name. But the inner section is different.

Here, this doesn’t refer to the obj. That’s why this.name is undefined. But the reference to the local variable self remains in scope and has proper access.

5. JavaScript return

Output

{ 
  name: 'shoaib' 
}
undefined

Why?

Here, funcOne returns the object successfully, but the problem is with funcTwo. Have a close look: funcTwo has a semicolon after the return. That means this will return nothing because of the semicolon.

6. Number.EPSILON

console.log(0.1 + 0.2);
console.log(0.1 + 0.2 == 0.3);

Output

0.30000000000000004
false

Why?

This is complicated. The result might be 0.3 and true, but it might not be. In JavaScript, numbers are all treated with floating-point precision and may not always yield the expected results.

What is the solution?

JavaScript introduced Math.abs for this. It will help you to compare the absolute difference between the two numbers.

7. Timeouts and Intervals

Output

1
4
3
2

Why?

  • The first and fourth lines console the output without any delay.
  • Then on the third line, timelimit comes and executes the log.
  • Finally, the ten-second delay is over and it prints the number 2.

8. Array reverse

Output

arrayOne length=7 value=m,e,h,e,d,i
arrayTwo length=7 value=m,e,h,e,d,i

Why?

  • With reverse in JavaScript, the method does not only return the reversed array. It also reverses the order of the array itself.
  • The reverse method returns a reference to the array itself.

For a better understanding, here is an example below:

9. Strict Equality

console.log(false == '0') 
console.log(false === '0')

Output

true 
false

Why?

In JavaScript, == compares between two things — and this is not strict. If we want to compare strictly, then we can use === instead of == and this will compare two things with their data types. Similarly, the != and !== operators work.

10. JavaScript Object

Output

undefined
shoaib

Why?

The first console is undefined because the method is extracted from the person object, so the identity function is invoked in the global context where the name property does not exist.

Conclusion

In this article, I tried to cover some tricky questions that programmers should understand in order to do better in their interviews. I will try to include more in the future. If I missed any interesting ones, please comment down below.

Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *