10 most common Javascript interview questions you should know

Asadujjaman Mridul
6 min readMay 8, 2021
Photo by Headway on Unsplash

1. What are ‘Truthy’ and ‘Falsy’?

According to MDN documentation,

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. And all values are truthy until they are defined as a Falsy.

Lets have an example,

let x;if(x) {
console.log("Truthy");
} else {
console.log("Falsy");
}
// Falsy

Since the variable is just declared and doesn’t have any value inside it, so the typeof(x) will be undefined. And undefined is considered false in terms of Boolean context. That is why we got ‘Falsy’ as output.

The list of all Falsy are as below —

  • The false keyword
  • 0 and -0
  • 0n (BigInt)
  • Empty string — “ ” , ‘ ’ and `` (string with length of greater than 0, will be Truthy)
  • null
  • undefined
  • NaN

All values except these will be treated as truthy.

2. What is the difference between ‘null’ and ‘undefined’ ?

Let’s begin with the official MDN documentation —

The value null represents the intentional absence of any object value. On the other hand, a variable that has not been assigned a value is of type undefined.

It means, if a programmer declares a variable and does not assign any value in that, then the primary value of that variable will be undefined.

let x;
console.log(x);
// undefined

But, if someone explicitly set a value as an absence of value for that variable, that would be null.

let x = null;
console.log(x);
// null

3. What is the difference between ‘==’ and ‘===’ ?

Javascript has two types of comparison operators, one is used for type converting comparison, and the other is for strict comparison.

1 == '1' // true

Though 1 and ‘1’ are two types of values but using ‘==’ will return true. This code will try to check their value first, if they are different in types, javascript will try to convert their type, and check if they are the same in value or not.

1 === '1' // false

On the contrary, === will execute like a strict father! Since those values are of two types, it will at once return false.

4. What is the difference between ‘call’ and ‘apply’ ?

If any object has some methods defined in its scope, other objects can apply those same methods as their own methods by using — bind, call, apply.

If we want to use call method, then we have to pass comma separated parameters.

const shirt = {
id: "120",
totalPrice : function(price) {
let tax = (.1*price);
return price + tax;
}
}
const pant = {
id: "210" }
console.log( shirt.totalPrice.call( pant, 1000 ) ); // 1100

As you can see, there was no function defined inside the ‘pant’ object, but we can lend it from ‘shirt’. Similarly, we can use apply instead of call.

const shirt = {
id: "120",
totalPrice : function(price) {
let tax = (.1*price);
return price + tax;
}
}
const pant = {
id: "210" }
console.log( shirt.totalPrice.apply( pant, [1000] ) ); // 1100

The difference is, we have to pass every parameter in an array for apply.

5. Explain setTimeout and setInterval

We all know that, Javascript is synchronous in nature. But we can explicitly make it behave asynchronously. Lets see an example —

console.log("1st");
console.log("2nd");
setTimeout( ()=> {
console.log("3rd");
}, 1000);
console.log("4th");
console.log("5th");
/*
1st
2nd
4th
5th
3rd
*/

The ‘3rd’ is printing 1000 ms or 1s later. When we used setTimeout method, it passes the function defined inside it, to the event queue. If the event stack is empty means there is nothing to run inside the stack, then the Javascript runtime will take first of the event from the queue and execute it, and so on. This process is called the Event Loop.

“Philip Roberts: What the heck is the event loop anyway” at JSConf EU 2014

setInterval works almost the same as the setTimeout — it will be doing the same task after an interval.

Note: The duration we passed in the function doesn’t mean it will execute the task exactly after that duration, rather it is the minimum time for executing the inside task. For the above example, if the event stack takes more than 10000ms or 10s to finish every single task and has no further function to execute, then it will execute the setTimeout function.

6. What does ‘scope’ mean in Javascript?

In Javascript, scope means the area of accessibility of a variable. There are two kinds of scope is available in Js. The first one is called local scope and the second one is global scope.

if(condition) {
let variable1 = 100;
}
function action(parameter) {
const variable2 = 1000;
}
function fun(param) {
const variable3 = 500000;
}
console.log(variable1); // error
console.log(variable2);
// error
console.log(variable3);
// error

These variables are called local variables. You can not access variable1 outside that conditional scope. Similarly, variable2 can not be used outside its own function nor the variable3 by action function. These local variables have a limited access across the whole document.

let variable1 = 0;
const variable2 = 0;
const variable3 = 0;
if(condition) {
variable1 = 100;
}
function action(parameter) {
variable2 = 1000;
}
function fun(param) {
variable3 = 500000;
}

But if we declared those variables outside the conditional scope or function scope, then they will be treated as global variables. They will have access to the entire document.

Note: But! If we use var instead of let or const then the scope for that variable will be hoisted from the current scope. We have discussed this topic in the next question.

7. What is ‘hoisting’ ?

Whenever we declare a variable or set values for a variable — using var, it will be hoisted up from the block scope the variable was declared. The formal definition for hoisting is —

Hoisting is JavaScript’s default behavior of moving declarations to the top.

function isWorking() {
for(var i=0; i<10; i++) {
var x = 10;
}
console.log(x);
// 10
}

As we can see, the variable x is still working fine outside the for statement (block scope). For avoiding confusion, we recommend to use let and const instead of var.

8. What are the key features of Javascript? or Why should we use Javascript?

According to MDN documentation —

JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. JavaScript is a prototype-based, multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.

So for answering the question, we can take important keywords like -

  1. Single-threaded (but by using the functionality of event loop, it can act asynchronously as well)
  2. Lightweight
  3. Interpreted
  4. Just-in-time compiled
  5. Prototype-based language
  6. Multi-paradigm
  7. Client-side language (but can be used in back-end as well)
  8. Dynamic language
  9. Oop supported
  10. Imperative
  11. Declarative

Problem solving related Questions

9. Write a function to calculate Factorial in a Recursive way

const factorial = n => { 
if( n == 0 ) {
return 1;
} else {
return n * factorial( n-1 );
}
}
console.log( factorial(5) ); // 120
console.log( factorial(6) ); // 720

10. Write a function for Fibonacci Series till nth element

const fibonacci = n => {
const series = [ 0, 1 ];
for( let i=2; i<n; i++ ) {
series[i] = series[i-1] + series[i-2];
}
return series;
}
console.log( fibonacci(10) ); // [ 0, 1, 1, 2, 3, 5, 8, 13 , 21, 34 ]

That’s a wrap for this article. Thank you for being this far. Have a good day!

--

--