Top 10 JavaScript Interview Questions and Answers

Mehadi Hasan
5 min readMay 8, 2021

1. What is Truthy and Falsie Value in JavaScript?

Every integer is true, except 0 (zero), and Every string are true except empty string. Also every undefined variable should be false. Please have look below example.

const age = 4;
const age = 0;
const age = "mehadi";
const age = "";
let age;
let age=NaN;
// Falsie value
// 0
// ""
// undefined
// null
// False
// Truthy value
// 0
// ' '
// []
// {}
const age = " ";
if (age.length > 0) {
console.log("condition is true");
} else {
console.log("condition is false");
}

2. What is the difference between null and undefined?

undefined means when declarer variable but if we don’t assign anything in the variable, then it will show undefined. another example is when we call a function then if we don’t return it then it will show undefined. undefined is a negative value or false value.

null means empty or nothing, not exist anything. We use null for checking whether there exist anything or not. Also there was anything and we collect from there and its empty for checking it we use null

let pakhi;
console.log(pakhi); //undefined
function add(num1, num2){
console.log(num1, num2); //undefined
}
const premik = {name:"smart", phone:45665}
console.log(premik.gf); //undefined

3. double equal (==) vs triple equal (===), what is the difference in it?

const first = 2;
const second = "2";
if(first == second){
console.log("condition is true"); //condition is true
}
else{
console.log("condition is false");
}
const first = 2;
const second = "2";
if(first === second){
console.log("condition is true");
}
else{
console.log("condition is false"); //condition is false
}

double equal operator ‘==’ only check the value of a variable, don’t check it’s type, but triple equal operator ‘===’ check both of its type and value of a variable

4. What is scope or block scope? What is hoisting?

function sum(first, second) {
let result = first + second;
return result
}
const
output = sum(3, 7);
console.log(
result); // shows error
console.log(output);
let bonus = 20;
function
sum(first, second) {
let result = first + second + bonus;
console.log(bonus); // 20
return result
}
const
output = sum(3, 7);
console.log(output); // 30
var a = 5;
if (true){
var a = 5;
console.log(a); // 5
}
console.log(a); // 5

In modern JavaScript ‘let’ and ‘const’ is a scope variable, when we declare a variable on the scope of curly brace {} it works only in it, but when we declare it outside curly brace it is global variable and we can use it every where in JavaScript

x=40;
console.log(x); //40
var x;

In the above code we decarded variable at last of all code, but JavaScript it brings to the first and it is called hoisting.

x=40;
console.log(x); //refferenceError:
let x;x=40;
console.log(x); //SyntaxError:
const x;

So, we can apply Hoisting only in normal JavaScript.

5. What is Closure, encapsulation?

function stopwatch(){
let count = 0;
return function(){
count++;
return count;
}
}
const clock1 = stopwatch();
console.log(clock1())
console.log(clock1())
const clock2 = stopwatch();
console.log(clock2())
console.log(clock2())

When we call a function, was created inside of this function, then it created a closure environment of this function. If we call it using different variable it shows same continues value of its different variable. That means for different variable it creates same environment.

6. How internet works, DNS server, hosting server, database

Interner works through IP address. When we call anyone with the phone, we just select the person name and dial call, but this name has a uniqe number we have saved previous, the call is going through by this number in the internet this is called IP address. IP means Internet Protocol. This is similar to a phone number. Every website has a unique IP address.

Server is a lots of computer without Monitor, In this pc all data is stored as like a shelf.

7. What is an API, what is the purpose of API?

API means Application Programing Interface, We can explain API by giving an example,

If we want to browse Facebook and see our post. We don’t able enter on Facebook directly. First we need to give our ID and password then Facebook check whether is this a valid user or not. If our id and password matched then Facebook allow us to enter in the Facebook and Facebook show our post and status. In a summary of process of giving id, password and matching and then show our post and status Process is called API.

The purpose of API is to Load data from the server.

8. What is HTTP request, Status code?

When we go to a restaurant and find our favorite to eat. We don’t know whether our favorite food have or not. In the website same thing happened. When we call for data we call in a promise, that response ok or not ok, that means we can get data or not this is called HTTP request.

In our browser in network tab different code is shown, when we get data it shows 200 (means ‘OK’); If our url is wrong then shows 404 (‘Not Found’) these code are the status code.

9. Difference between GET and POST

10. Difference between bind, call and apply

There is a method (or function) in an object. If we want to use this by borrowing it one place to another place we can use call, apply, bind method.

Call — It first call the previous method of the previous object then it passes argument on which it will be applied and then it passes the parameter as comma (,) separation.

Apply— It first call the previous method of the previous object then it passes argument on which it will be applied and then it passes the parameter as an array.

Bind— It first call the previous method of the previous object and it bind new object on this previous method and then it call how much its needed.

const normalPerson = {
firstName: 'Rahim',
lastName: 'Uddin',
salary: 15000,
getFullName: function(){
console.log(this.firstName, this.lastName);
},
chargeBill: function(amount, tips, tax){
console.log(this)
this.salary = this.salary - amount-tips-tax;
return this.salary;
}
}
const heroPerson = {
firstName: 'Hero',
lastName: 'Balam',
salary: 25000
}
const friendlyPerson = {
firstName: 'Hero',
lastName: 'Golam',
salary: 24000
}
normalPerson.chargeBill();
const heroChargeBill = normalPerson.chargeBill.bind(heroPerson)
heroChargeBill(2000);
heroChargeBill(3000);
console.log(heroPerson.salary);
console.log(normalPerson.salary);
const friendlyChargeBill = normalPerson.chargeBill.bind(friendlyPerson);
console.log(friendlyPerson);
normalPerson.chargeBill.call(friendlyPerson, 900, 100, 10);
console.log(friendlyPerson.salary);
console.log(normalPerson.salary);
normalPerson.chargeBill.apply(heroPerson, [3000, 300, 90]);
normalPerson.chargeBill.apply(heroPerson, [4000, 400, 90]);
console.log(heroPerson.salary)

--

--

Mehadi Hasan

Hello, I am Mehadi Hasan, Frontend React web Developer. From my little knowledge about JavaScript I am trying to publish somethings.