10 Commonly Asked Interview Questions in JavaScript

What are Falsy values and Truthy values?

Values that generally returns false is called falsy values in JavaScript. And some falsy values are : “”, 0, undefined, NaN, null, False etc.

Values that generally represents true are truthy values in JavaScript. Some truthy values are : “ ”, ‘0’, [] ,{}, true, 1.

Look closely, here “” an empty string is falsy but a string with just an empty space represents truthy value. Because a space is still something.

What is the main difference between ‘null’ and ‘undefined’?

I will make it easy for you by giving some examples.

Undefined :

a. Not assigning a value in a variable = undefined.

b. Not returning from a function = undefined.

c. Not passing the necessary values in a function = undefined.

d. Trying to access undeclared properties from an object = undefined.

Null :

Null means absence of value. We set this explicitly to any variable.

There are some other cases too where both cases happen but if you can remember these points and answer them in the interview when asked, you are good to go.

What is Scope?

Scope determines accessibility or visibility of variables. A global value can be accessed from anywhere within the code. But a local variable can only be accessed from the local scope only.

For example :

let c = 10

function add(){

let a = 10;

let b = 20;

console.log(c); //This can be accessed from anywhere within the code.

return (a+b);

}

//console.log(a); This is not possible cause it can be accessed within the function only.

console.log(c);

What Is Closure?

If a function is declared within another function and we call the upper level function along with the function in it, then they both can have values of their own. This is called closure in JavaScript . For example :

function makeAdder(x) {

return function(y) {

return x + y;

};

}

var add5 = makeAdder(5);

var add10 = makeAdder(10);

console.log(add5(2)); // 7

console.log(add10(2)); // 12

Here both of them have different values of their own.

Call ,Apply and Bind

If I have a method within and object and I want to use it on another object then I can use call(), apply() and bind()

call(): It just calls the method and we can pass the object we want to work with. We can also pass several data as coma separated argument.

apply(): This is almost same as call(). The only difference is after first argument the all other arguments have to passed in an array.

bind(): In this process the method has to be binded with bind(), then we can use it again and again. It mainly returns a function which can be invoked later.

What is Global and Window?

Global : Variables defined outside the functions. And they can be accessed from anywhere inside the function.

Window : In short Window is JavaScript running environment. It can also be accessed form anywhere.

If I don’t put variable type by mistake then the variable becomes global.

And window.variable_name = global.

What is ‘this’?

‘this’ keyword confuses every developer at any point in their life. Mainly ‘this’ refers to the object it belongs to. Or more clearly, it will contain the value of the context it is running on in the moment. For example,

const myTest= {
value: 20,
function: function() {
return this.value;
},
};

console.log(myTest.function());
// expected output: 20

if ‘this’ has no context then it belongs to the window context.

What is JavaScript Event Loop?

In event loop JavaScript maintains a stack and it executes the stack one after another synchronously. After finishing the stack it then executes asynchronously waiting list works. It can be understood by the diagram below:

It shows that JavaScript actually makes a stack of works it has to do and then executes one by one from the top of the stack.

What Is Node?

It is a common interview question. And many of as answer it wrong. No Node is not any library. It is a JavaScript runtime environment. Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Nowadays Node.js is vastly popular among millions of developers in the world. Because it can handle many concurrent requests at a time and execute all of them very efficiently.

Show First 10 Values of Fibonacci Series by Recursive Function

function fibonacci(n){

if(n == 0){

return [0];

}

else if(n == 1){

return [0, 1];

}

else{

var fibo = fibonacci(n-1);

var nextElement = fibo[n-1] + fibo[n-2];

fibo.push(nextElement);

return fibo;

}

}

var result1 = fibonacci(10);

console.log(result1);

Expected result :

[0,1,1,2,3,5,8,13,21,34]

Web Developer, Dreamer, Quick Learner.