10 most frequently asked javascript interview questions and their explanation
Ok, so you may be learning javascript for quite a while and you are going to face an interview very soon. Then this article can give you some hints about what you are going to face. So, brace yourself and go through this article. We will discuss some important questions in this article.
1. null vs undefined
value of a variable can be null or undefined. ‘null’ is set explicitly and we can get undefined in various ways such as no value set for a variable. Let’s see a few ways how we can get undefined.
var name;
console.log(name); // output: undefinedvar obj = {name: 'abu', phone:12345}
console.log(obj.age); // output: undefined :: as there is no age property in the objectfunction add(first, second){
console.log(first+second);
}
var result = add(10,5);
console.log(result);
// output: 15
// undefined :: because nothing is returned from the function var age = null;
console.log(age); // output: null
2. == vs === (double equal vs triple equal)
Let’s talk about this == (double equal). Double equal (==) in javascript is used for comparing two values but it ignores the datatype of variables.
var num =0;
var str='0';
if(num==str)
console.log("matched");
else
console.log("didn't match");//output: matched
‘===’ this operator also checks datatype and compares two values.
var num =0;
var str='0';
if(num===str)
console.log("matched");
else
console.log("didn't match");//output: didn't match
3. var vs let vs const
var- declarations are globally scoped or function scoped, they can be updated and re-declared within its scope.
let- block-scoped variable, they can be updated but can’t be re-declared within its scope.
const- block-scoped variable, they can’t be updated nor re-declared within its scope.
4. Hoisting
This one is a bit tricky. To put it simply- variable and function declarations are physically moved to the top of your code. But this is not the case. Actually, the variable and function declarations are put into memory during the compile phase but stay exactly where you typed them in your code. Example-
namePrinter('abu');
function namePrinter(name){
console.log("the name is"+name);
}
//output: the name is abu
although we called the function before its declaration, it will show output because of hoisting. One thing to note here- JavaScript only hoists declarations, not initializations. If a variable is declared and initialized after using it, the value will be undefined. Example-
console.log(num); // Returns undefined, as only declaration was hoisted, no initialization has happened at this stage
var num; // Declaration
num = 6; // Initialization
5. normal function vs arrow function
There are a few differences that should be noted. We will discuss them one by one.
this keyword
Inside a normal function, this keyword works as a global object, and inside an arrow function it doesn’t. Example-
fruit = "mango";function getFruit(){
return this.fruit;
}
console.log(getFruit()); //output: mangoconst arrowFruit(){
return this.fruit;
}
console.log(arrowFruit()); //output: undefined
constructor:
The regular function can easily construct objects. Using new keyword instance of a function can be created.
function getFruit(fruit) {
this.fruit = fruit;
}
const newFruit= new getFruit('mango');
newFruit instanceof getFruit; // true
But the arrow function doesn’t support constructors. Example
const getFruit = (fruit) => {
this.fruit = fruit;
}
const newFruit= new getFruit('mango'); //TypeError: getFruit is not a constructor
6. truthy and falsy values
truthy values are those for which a condition is true and falsy values are those for which a condition is false.
Falsy values: 0 , “”-empty string , NaN, undefined(let example;) , null
Truthy: any number, non empty string ‘0’ , [array, may be empty] , {object, may be empty}