Tricky JavaScript interview questions

Tricky JavaScript interview questions

ยท

4 min read

In this article, you will see a lot of JavaScript tricky questions maybe you will face in your interview.

Let's get started.


console.log(1 + "1")// 11
console.log(1 - "1") // 0

1- Plus operator in JavaScript

The first thing you should think about is why this is the answer is why didn't be 2. I will tell you why because in JavaScript plus operator between two numbers use to add two numbers but if we do not have two numbers is used to concat them but in the second operation we get 0 because the minus operator in JavaScript is only used with numbers which means it will not work on any string so JavaScript will convert any string use with a minus operator to numbers.


let arr = [1,2,3,2];

let newArr = [...new Set(arr)]; // [1, 2, 3]

2- Remove duplicates from array

In this question, you have an array with some numbers and you should generate a new array without any duplicate values and in a single line.

Here you use the new data structure called Set.



let func = function() {

    {
        let l = 'welcome';
        var v = 'hello';
    }
    console.log(v); // var
    console.log(l); // l is not defined
}

func();

3- Block Scope in JavaScript

Here I have a function and block scope with two variables one use let and another use var what do you think about output here?

I will talk with you about the result. In JavaScript var within any block scope like for or if which will be global opposite let. let within any block scope will be local so in case var output will be hello but in case let output will be let is not defined.


console.log(5 < 6 < 7) // true
console.log(7 > 6 >5) // false

4- Comparison Operators

When I first looked at this question I say two operations will be true but there is something behind it that I did not see it.

When you logical operator it will convert false to zero and true to one so in this question when we look to first operation 6 greater than 5 this will give you true and convert to one and compare with 7 do one less than 7 Yes that is true.

In the second operation when we look to first operation 7 greater than 6 will give you true and convert to one and compare with 5 do one greater than 5 No that is false.


let person = {
    name: "Leon",
}

5- Prevent users form adding items to object

The question here is how to prevent user from adding new items to object or modifying old items.

You can use Object.freeze(person) but if you only want to prevent a user from adding a new item and can modify an old you can use Object.seal(person).


let x = [1, 2 ,3];
   x[-1] = -1;
   console.log(x[x.indexOf(10000)]);

6- Search about item in an array

JavaScript has a lot of array methods and IndexOf method is one of them.

In an array method indexOf return the first appearance to value in an array or -1 if this value there is not. so here x.IndexOf(10000) will be return -1 and x[-1] will be return -1 from array.


console.log(num1);
console.log(num2);
var num1 = 5;
let num2 = 6;

7- Hoisting in JavaScript

JavaScript has a concept called hoisting means it takes all variables and put them at the top of the file.

In this question num1 = undefined and num2 gives us an error, why.

Because hoisting when it works takes declaration variables to the top of the file and var allows access before initializing but let don't allow that, so num1 = undefined and num2 gives us an error.

Example.

var num1 = 5

// In javascript it breaks down into two-part declaration and assignment.

var num1; // Declaration it is placed at the top of. the file.
num1 = 5; // Assignment stays in place.

So if we want to print num1 will give us undefined.

Summary

There are a lot of tricky questions in JavaScript that we need to check out and know what is the output on it so I will try to collect a lot of questions that maybe we will face in interviews in the future.

If you enjoyed this article kindly drop a comment. You can always connect with me on Twitter๐Ÿ˜ƒ.

ย