Top 10 Tricky Javascript Questions often asked by Interviewers

Andreas Sujono
8 min readDec 17, 2022

Top 10 tricky Javascript questions often asked by interviewers. Make sure you know all of them!

If you apply for a Frontend or web developer role, you might be assessed for your javascript skills. There is a lot of JS concept that is quite tricky such as hoisting, closure, data types, asynchronous, and others. Here are some of the most frequently asked and trickiest Javascript Questions:

1) Mutability

In Javascript, we have 7 primitive data types (string, number, bigint, boolean, undefined, symbol, and null). These all are immutable meaning that once a value is assigned, we cannot modify them, what we can do is reassign it to a different value (different memory pointer). The other data types such as Object and Function, on the other hand, is mutable meaning we can modify the value in the same memory pointer.

//Q1
let text = 'abcde'
text[1] = 'z'
console.log(text) //ans: abcde

String is immutable, so once assigned to a value, you cannot change it to a different value. What you can do is reassign it. Remember that changing value and reassigning to another value is different

//Q2
const arr = [1,2,3]
arr.length = 0
console.log(arr) //ans: []

Assigning arr.length as 0 is the same as resetting or clearing the array, so right now, the array will become an empty array

//Q3
const arr = [1,2,3,4]
arr[100] = undefined
console.log(arr, arr.length) //ans: [ 1, 2, 3, 4, <96 empty items>, undefined ] 101

Because an array consumes a contiguous memory location, when we assign the index 100 to a value (including undefined), JS will reserve the memory from index 0 to index 100. This means the array length now is 101

2) Var and Hoisting

//Q4
var variable = 10;
(() => {
variable2 = 100;
console.log(variable);
console.log(variable2);
variable = 20;
var variable2 = 50;
console.log(variable);
})();
console.log(variable);
var variable = 30
console.log(variable2);
//ans:
//10
//100
//20
//20
//ReferenceError: variable2 is not defined

var is a functional scope variable, while let and const are block-scoped variables. Only var can be hoisted…

--

--

Andreas Sujono

A Full Stack Developer with 5 years of working experience, Web2 and Web3 Enthusiast, twitter: @AndreasSujono2