Most Common Interview Questions You Must Know

Md. Mahamudul Hasan
5 min readMay 8, 2021

1. Truthy and Falsy values

In JavaScript, a truthy value is a value that when encountered in a Boolean context is considered true. All values are truthy except for false, 0, -0, null, undefined and NaN. These values are defind as falsy.

Examples of truthy values in JavaScript (which will be coerced to true in boolean contexts, and thus execute the if block):

if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)

2. Null Vs Undefined

When you define a variable but not assign a value to it, it automatically puts a placeholder which is called undefined. JavaScript automatically does it for you. You don’t have to do it manually.

Null means an empty or non-existent value. A null value is explicitly meaning nothing, whereas an undefined value typically means that a variable has been declared but has not yet been defined.

var a;
console.log(a);
// undefined

var b = null;
console.log(b);
// null

Null and undefined are both primitives and falsy values. However null is also an object.

3. double equal (==) vs triple equal (===)

double equal (==) is defined as Loose Equality Comparison. Loose equality compares two values for equality after converting both to a common type. After conversion (one or both sides) to a common value type, a loose equality comparison is finalized as === does.

undefined and null are loosely equal; that is, undefined === null is true, and undefined ==null is true

const number = 0;
const bigger = 0n;
const string = '0';

console.log(number == string); // true
console.log(bigger == number); // true
console.log(string == bigger); // true

triple equal (===) is defined as Strict Equality Comparison. The values are considered equal if they have the same type, are not numbers, and have the same value. If they don’t have the same type, are not numbers, and have the same value, then they are considered unequal. Also numbers are considered equal if they are both non-Nan and have the same value, or if one is +0 and one is -0.

var number = 0;
var object = new stringing('0');
var string = '0';
console.log(number === number); // true
console.log(object === object); // true
console.log(string === string); // true
console.log(number === object); // false
console.log(number === string); // false
console.log(object === string); // false
console.log(null === undefined); // false
console.log(object === null); // false
console.log(object === undefined); // false

4. Scope

There are two types of function scopes one is local and another is global.

In a JavaScript function, variables declared within the function have Local scope. They can only be accessed within the function.

// code here can NOT use phoneName

function myFunction() {
var phoneName = "Samsung";

// code here CAN use phoneName

}

Declaring a variable outside of a function, makes it global and can have Global Scope. Any script or function on the same page can access it.

var phoneName = "Samsung";
// code here can use phoneName

function myFunction() {
// code here can also use phoneName
}

There is another scope called Block Scope. If a variable is declared with the let keyword, then it can be in a block. Variables declared in a block can be accessed only from within the block.

{
let y= 5;
}
// 5 can not be used here

5. Array.prototype.reduce()

reduce() returns a single value for each element in the array. It works by executing a reducer function (that you specify) on each element.

const array1 = [5, 6, 7, 8];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 5+6+7+8
console.log(array1.reduce(reducer));
Output: 26
// 1+5+6+7+8
console.log(array1.reduce(reducer, 1));
Output: 27

6. call, bind and apply

call() method calls a function with a ‘this’ value and arguments one by one. We can then explicitly specify the ‘this’ in the calling function.

var friendl = {firstName: 'Mahmudul', lastName: 'Hasan'}; 
var friend2 = {firstName: 'Faysal', lastName: 'Ahmmed'};

function invite(greetingl, greeting2) {
console.log(greetingl +' '+ this.firstName +' '+ this.lastName+ ', '+ greeting2);
}
invite.call(friendl, 'Hey', 'What do you do?');
Output: Hey Mahmudul Hasan, What do you do?
invite.call(friend2, 'Hey', 'How are you?');
Output: Hey Faysal Ahmmed, How are you?

In bind() method you can pass an array and any number of arguments into bind() to get a new function.

var friendl = {firstName: 'Mahmudul', lastName: 'Hasan'}; 
var friend2 = {firstName: 'Faysal', lastName: 'Ahmmed'};

function invite(greetingl, greeting2) {
console.log(greetingl +' '+ this.firstName +' '+ this.lastName+ ', '+ greeting2);
}
var inviteFriend1 = invite.bind(friend1);
var inviteFriend2 = invite.bind(friend2);
inviteFriendl('Hey', 'What do you do?');
Output: Hey Mahmudul Hasan, What do you do?
inviteFriend2('Hey', 'How are you?');
Output: Hey Faysal Ahmmed, How are you?

You can pass arguments as an array into this function’s apply( ) method. It Invokes the function, providing you with a list of arguments.

var friendl = {firstName: 'Mahmudul', lastName: 'Hasan'}; 
var friend2 = {firstName: 'Faysal', lastName: 'Ahmmed'};

function invite(greetingl, greeting2) {
console.log(greetingl +' '+ this.firstName +' '+ this.lastName+ ', '+ greeting2);
}
var inviteFriend1 = invite.bind(friend1);
var inviteFriend2 = invite.bind(friend2);
invite.apply(friend1,['Hey', 'What do you do?']);
Output: Hey Mahmudul Hasan, What do you do?
invite.apply(friend2,['Hey', 'How are you?']);
Output: Hey Faysal Ahmmed, How are you?

7. Understanding ‘this‘ keyword

This keyword refers to an object which is executing javascript code currently. That is, every javascript function which is executed has a reference to its current execution context, called this. Execution context means it is called from this context.

function test() { 
console.log("Simple function call");
console.log(this === window);
}
test(); //prints true on console
console.log(this === window) //Prints true on console.

8. DOM (Document Object Model)

Browsers create a Document Object Model of a web page when it is loaded, which consists of a tree of Objects:

DOM

JavaScript can create dynamic HTML with the object model. It can change all the HTML elements, HTML attributes, CSS styles in the page.

9. API

API means Application Programming Interface. APIs let applications interact with external software or operating systems, server-side applications, or microservices. A user responds to an API, and the API sends back the system’s response back to the user. For example, you add something to your cart.

Developers use API to make “calls” and “requests” to send or receive information through a programming language called “JSON.” It can also perform defined actions such as updating or deleting data.

Requests can be made with API using four basic methods:

  • GET — Gathers information (Pulling all Coupon Codes)
  • PUT — Updates pieces of data (Updating Product pricing)
  • POST — Creates (Creating a new Product Category)
  • DELETE — (Deleting a blog post)

10. difference between var, let and const

Here I attached a simple table to understand the diferences between var, let and const. ES2015 introduced two important new JavaScript keywords: let and const.

https://twitter.com/fanny_codes/status/1289561087206203393

--

--