Top 10 JavaScript interview questions

interview question

MD Sakib Ahmed
3 min readMay 8, 2021

Global scop VS Local scop

When we declare a global variable, it means this variable has global scope. We can access this variable from anywhere of our application. Local scope means that the variable and function of the particular scop are only accessible from this scope not anywhere. function is a local scope and curly braces is local scope in let and cost. Thus, expression is local scope in let and const.

Difference between null and undefined

When we don't assignment a value (except const) it is automatically assigned to undefined value. So undefined means that unintentionally that variable is missing a value. In other hand null means that intentionally this variable is missing value. We set the null value to variable. null is an object if you check type of null, you get it is an object.

Difference between dubble equality (==) operator and triple equality (===) operator

dubble equality operator only check the equality of two values after necessary conversion between twos. So, 1 == “1” return true. triple === operator checks both data type and value. So, 1 === “1” return false. But 1 == 1 return as like 1 === 1 return true. tiple equality === operator also called strike equality operator.

What is closure in JavaScript?

closure is a feature of JavaScript language itself. We don't need to create this implicitly rather it is created automatically in our code. When a function is created in another function the nested function keeps the reference of any variable or function those are located in the outer function even the outer function is not in execution stack, it is a closure. By the power of closure, we can create so powerful program in our application.

How ‘this’ key word works in JavaScript?

When ‘this’ key word is used in a function, it refers to window object. When it is used in an object method it refers to the current object. But if we write a nested function in object method, ‘this’ key work in the nested function refers to global object not the current object. for example:

function fnc() { 
console.log(this) // global object (window)
}
const obj = {
name: "sakib",
age: 23,
calcalutaAge : function () {
console.log(this) // obj
setTimeout(function (){
console.log(this); // global object
},1000)}}

bind , call and apply method in JavaScript?

bind,call and apply are built-in method of function in JavaScript. It sets ‘this’ value to the first argument of these method and second argument to argument of the function. bind just creates a function with first Argument. You have to call it implicitly with necessary arguments. Call and apply immediately calls that function after create. when you call “call” method you have to set arguments with comma separator. When you call “apply” method you have to set arguments as an array. That is the difference between call and apply and also bind.

How JavaScript is asynchronies even after it is single thread language?

Web browser provides us some API that we use to create asynchronies JavaScript behavior in JavaScript like setTimeout, setInterval, fetch etc. The above methods are given from browser not from JavaScript language. So, it is 100% true that JavaScript is single thread language but even it is single thread we can perform asynchronies behavior by the help of some web API provided by browser.

Remove duplicate item from an array (problem solving)

Remove duplicate item from an array can be in several ways. But i am doing it using for loop

const duplicateArray = ["sakib","rafid","radwan","radwan","sakib"];const uniqName = []for (let i = 0; i < duplicateArray.length; i++) {const element = duplicateArray[i]const index = uniqName.indexOf(element)if(index === -1){uniqName.push(element)}
}
console.log(removeDuplicateArray);

Calculate factorial in recursive function (problem solving)

Here is the solution of this problem in recursive function: -

function factorial(number) {if(number === 1){return 1}return number * (factorial(number -1))}
console.log(factorial(4)); //24

Create febonacci series using for loop (problem solving)

Here is the solution of this problem: -

let febo = [0,1]for (let i = 2; i <= 12; i++) {febo[i] = febo[i-1] + febo[i-2]}console.log(febo);

--

--

MD Sakib Ahmed
0 Followers

I am a MERN stack web developer. I love programing. Learn new tanchnology is my hobby