Skip to main content

Javascript Interview Question and Answer - 3

In this post, I have discussed 5 basic javascript question and answer that is mostly asked in the interviews. Most of the time interviewer starts the interview with some of these basic questions.
So here we have

Q1. What is the difference between == and ===?

Solution : When we want to test the equality of two values(LHS and RHS), we use == or ===.
== is basically used to check if the two values are equal or not,and we are not considering their datatype. e.g.
if we check

5 == "5"   // true as values are equal
undefined = null   //  true as both represents NO VALUE

In case of ===, we not only consider the values but the datatype as well.e.g.

5 === "5"   // false as though values are equal but datatypes are different

Similarly

undefined === null  //  false, as datatype of undefined and null is undefined and object respectively

It is highly recommended to use === in the programming as we get very unexpected results out of ==. e.g.

false == 0   // true
false == "false"   // false
0 == ""  //  true
"0" == "" // false

Q2. What will be the output of the following code snippet?

console.log(1);
setTimeout( () => console.log(2),1000);
setTimeout( () => console.log(3), 0);
setTimeout( () => console.log(4), 0);
console.log(5);

Solution : We will get
1
5
3
4
2
Since console.log are simple calls we get 1 and 5. The moment JS engine see setTimeout function, it will put them in event stack.
And once it is done with the execution of the main function, it will bring back these setTimeout from the stack. Now wince console.log for 3 and 4 has time 0, we get 3 and 4 after 1 and 5, and after a second it will print 2 as well.
Learn about arrow function.

Q3. What will be the output of the following code snippet?

for(var i = 0 ; i < 10 ; i++){
    setTimeout( () => console.log(i), 1000)
}

Solution : We might be expecting result from 0 to 9, but we will actually get 10 printed 10 times.
The reason is during the loop execution the setTimeout will be put to event stack and once the for loop end, all the setTimeout will be executed with the current value of i, i.e. 10 because the last value of i was 10 when loop ended.
We can avoid this issue by using ECMAS6 let instead of var in for loop.

Q4. What will be the output of the following code snippet?

var a = 5;
var hello = () => {
       console.log(a);   // ?
       var a = 10;
}
hello();

Soltion: It will log undefined in the console. It will not take the value 5 from the global scope, since it has a variable with same name in its local scope. It will not give error because of hoisting. It won't log 10 since assignment is after the console.log. Check hoisting article.

Q5. What will be the output of the following code snippet?

let a = [1,2,3];
let b = a;
b.push(4);
console.log(b);
console.log(a);

Solution: for b it will log [1,2,3,4] and for a as well it will log [1,2,3,4] because array are referenced type. Any changes on either of the array will reflect on other.

We can avoid this by doing the assignment of one array to other using spread operator( case in ECMAS 6). i.e.

let a = [1,2,3];
let b = [...a];
b.push(4);
console.log(b);    // [1,2,3,4]
console.log(a);    // [1,2,3]

Thank you!!

Comments

Popular Posts

Come Closure in Javascript

What is a closure? If we want to derive a definition,we can say, A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has: access to its own scope (variables defined between its curly brackets and its parameters),access to the outer function’s variables and parameters, access to the global variables. Now we will look into it in detail. Consider following example
var globalVar = "xyz"; function outerFunc(outerArg) { var outerVar = 'Alex'; function innerFunc(innerArg) { var innerVar = 'Chris'; console.log("globalVar = " + globalVar); console.log("outerArg = " + outerArg); console.log("innerArg = " + innerArg); console.log("outerVar = " + outerVar); console.log("innerVar = " + innerVar); } innerFunc(456); } outerFunc(123); In this example, i…

Javascript Hoisting Interview Question and Answers - 5

In this post,I have tried to cover all the types of possible interview questions and answers on hoisting. If you are not familiar with the hoisting, I would highly recommend you to go to my post on hoisting. Once you are comfortable with the topic, you can attempt these questions. Understanding the type of question will help you to answer. I have provided the hint after each question. All the answers are given at the end of the post.
//Question 1 console.log('bar:', bar) bar = 15 var foo = 1 console.log(foo, bar) var bar Hint: Basics of hoisting //Question 2 var foo = 5 console.log('foo:', foo) var foo; var bar = 10; var bar; console.log('bar:', bar) var baz = 10 var baz = 12 console.log('baz:', baz) Hint: Basics of hoisting //Question 3 function foo() { function bar() { return 5 } return bar() function bar() { return 10 } } console.log(foo()); Hint: Think what happens if two functions wit…

Javascript Interview Question and Answer - 1

In this article we are going to discuss 2 javascript interview question and their answers.These questions are highly asked in the Javascript interviews.

Disclaimer: - You might have different solution working with all test cases for both the problems.

So lets discuss the first question.This question is to test your ability in terms javascript object knowledge and your logical thinking.

Q1. You have been given a nested object "Person" which has one of the property as "password".Write a generic function to change the value of all the "password" property to "*****". Object is nested and may contain other object with property "password" and so on. Here is a sample object.

var Person = {
     name: 'Alex',
     age : 40,
    password : 'abc',
    x : {
       name : 'Chris',
       password: 'abcd',
       y : {
          name : 'Mike',
          password: 'abcd',
         },
     }
.
.
}

Before we …