Most Common JavaScript Interview Question & Answer
If you’re attending a JavaScript web developer interview there will be JavaScript conceptual and coding-related questions. In this article, I'm going to provide some of the frequently asked JavaScript interview questions.
So without delay let's dig right into the questions.
Explain Truthy vs Falsy Values
In JavaScript, Truthy is a value that is true
in boolean context. And Falsy is a value that is false
in boolean context. Some of the Truthy values are 5,'10', true, infinity, {}, [], '0' etc
Some of the Falsy Values are false, 0, -0, “”, null, undefined
. For example, we can check conditions using Truthy and Falsy values.
// Truthy
if(true){
console.log('Truthy'); // Returns Truthy
}
if('0'){
console.log('Truthy'); // Returns Truthy
}// Falsy
if(false){
console.log('Inside If'); // No return
}
else{
console.log('Falsy'); // Returns Falsy
}if(NaN){
console.log('Inside If'); // No return
}
else{
console.log('Falsy'); // Returns Falsy
}
Null vs Undefined different ways to get Undefined
undefined
is a primitive type like Numbers, Objects, etc In JavaScript. Undefined means a variable is declared but its value hasn't been assigned. There are different ways to get undefined
one of them is to not assign any values to a variable then it will return undefined
If a function does not return a value if we console.log the function it will return undefined.
If we do not set a default value, any unmatched parameter that passes through a function will return undefined.
null
is an assigned value in JavaScript. You can assign null
to any variable, array, object, etc.
// undefinded
var test
console.log(test); // Returns undefined
alert(test); // Returns undefinedconst sum = (num1, num2) => {num1 + num2};
console.log(sum); // Returns undefined
sum(5); // Returns undefined
// null
var test2 = null;
console.log(null); // Returns null
Triple Equal (===) VS Double Equal (==)
Triple Equal and Double Equal are conditional operators. They are used to compare between two or more types in javaScript.
In JavaScript, Triple equal ===
is defined as strict equity. This operator compares not only the values but also the types of the variable in an expression. Neither of any values is converted before comparing.
On the other hand, Double Equal ==
is defined as loose equity. This operator converts given values into a common type and then compares them. According to ECMA Script (ES6), all primitive types are loosely equal means undefined == null
.
// Triple Equal ===
var num = 0;
var obj = {name: 'Spider Man'};
var str = 'Hi';
console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true
console.log(num === obj); // false
console.log(num === str); // false
console.log(obj === str); // false
console.log(null === undefined); // false
console.log(obj === null); // false
console.log(obj === undefined); // false// Double Equal ==
const num2 = 0;
const big = 0n;
const str2 = '0';
const obj2 = {name: 'Super Man'};
console.log(num2 == str2); // true
console.log(big == num2); // true
console.log(str2 == big); // true
console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // trueconsole.log(undefined == null); // true
Scopes in JavaScript
In JavaScript, scopes determines the accessibility and availability of a variable or a function or other statements in a specific area of your code. Scopes usually defined with braces {},(),[]
.Some of the most common scopes are
1. Global Scope
Global scope is the area outside of all the JavaScript function. JavaScript has only one Global Scope.
// Global Scope
const name = 'Iron Man';const superHero = () => {
console.log(name); // name variable can be accessed here
}
2. Local Scope
Variables declared inside a function are only accessible inside that function. These variables are called local variables.
Local Scope can be Devided into two scopes Functional Scope and Block Scope.
Functional Scope is the area inside the hole function and any variables declared within this scope can be accessed.
Block Scope is the area inside a block such as if else
for, while, do while
switch case
etc. Typically this area is defined by {}
braces.
Any variable declared within the block scope cannot be accessed outside of it.
// Local Scopeconst superHero = () => {
const hero = 'Batman';
console.log(hero); // Shows Batman as a local variable
if(true){
console.log(hero) // Shows Batman as a local variable
}
}
superHero();
console.log(hero); // Shows Error: hero is not defined// Functional Scopeconst superVillain = () => {
const villain = 'Joker';
console.log(villain); // Shows Joker as a local variable
}// Block Scopeconst human = 'Jack';
console.log(human); // Shows Jack as a Global variable
if(true){
const human2 = 'Will';
console.log(human2); // Shows Will as a Block Variable
}
console.log(human2); // Shows Error: human2 is not defined
Lexical Scope
Another Scope in JS is Lexical Scope which states that all the scopes inside a parent scope has access to all of the variables declared inside that parent scope.
// Lexical Scopeconst avengers = () => {
const ironMan = 'Tony Stark';
const captainAmerica = 'Steve Rogers';
for(i=0; i<5; i++){
// Lexical Scope here
const winterSolder = 'Bucky';
console.log(ironMan); // Shows Tony Stark 5 times
}
console.log(winterSolder);// Shows Error:winterSolder is not defined
}
avengers();
Closueres In JavaScript
Closure is a function bind together with its lexical Environment or scope.
A function defined inside a parent function can access to all the variables inside that parent function so when we call that mother function the child function forms a closure which can be used later on in the code.
// Closure
function a(){
var num = 10;
function b(){
console.log(num);
}
return b;
}
var c = a(); // Here Function a creates Function b which forms a closure and stored inside c variableconsole.log(c) // Returns b(){
console.log(num);
}
c(); // Returns 10 although function a is earased function b is stored inside memory by Closure as y = closure(x)