10 interview questions every JavaScript developer should know

Md Rishad Khan
4 min readMay 8, 2021

We face a lot of js questions in interviews, and I’m trying to help you out by answering some important questions in the section below.

Truthy and Falsy values

When testing conditions in code (if…. else), we need to know which one is true and which one is false.

Falsy Values

  1. 0
  2. undefined
  3. null
  4. NaN
  5. “” (empty String)
  6. false

Truthy Values

  1. ” “ (space with string)
  2. [] empty array
  3. {} empty Object
  4. true

It’s simple to code to remember all of these values, and it’s simple to define error.

Null Vs Undefined

We learned how to find undefined at the start. Undefined can be found in a variety of ways, the first of which is by not assigning a value to a variable.

let a; // output is undefined

When we call a function but don’t return the value within the function, the result is undefined.

function add (a , b){

sum = a + b ;

}

//output is undefined

When we try to access a property on an object but the property is not available, the result is undefined.

Scope

If we assign a variable to a function and try to call it from outside the function, the variable would be undefined since we are attempting to access scope from outside the function. When we assign a variable outside of a function and attempt to call it from the inside or outside, the output will appear since it is called from the global scope. When we declare variables in conditional statements (like if…else) and try to access them from output, it fails because it is a block scope.

Difference between bind, call and apply

We must understand how this works then we’ll be able to distinguish between them.

bind

We make an object, which has a method in it. Using bind, we can apply this method to other users. Basically, we borrow and use methods from the main user.

call

We can call without using the call function, and we can transfer multiple values.

apply

The only difference between calling and applying is the argument. As an array, the argument is sent.

setTimeout vs setInterval

setTimeout is used when a function is called only once and for a certain amount of time. setTimeout runs the entire code for a specific time period, but a small piece of code within serTimeout would call the time period later.

setInterval is similar to setTimeout, except that it runs indefinitely. To close the infinity loop, another function is needed to stop this function.

How JavaScript works event loop stack and queue

The call stack will keep track of all activities to be carried out. The call stack is When a feature has been completed, the stack is raised and sent to the event queue. The language is single-threaded, but the APIs in the browser are different threads. Continuously, the event loop tests whether the call stack is null or not. New functions from the event queues will be inserted if they are empty.

Find the largest element of an array

A popular concept in an interview on problem solving. This is a typical issue on the interview board. I am going to solve and expand this problem

let marks = [44 , 45 ,88 , 64, 99 ,42, 66 , 91 , 78 ,88];

let max = marks[0];

for(let i=0; i<marks.length;i++){

let element = marks[i];

if (element > max){

max = element;

}

}

console.log(max);

You have to compare this code with the store value, then you can easily find the largest element.

Event Bubble

We need an example to understand event bubble.I’ll show you an example below.

<body>

<div>

<ul>

<li>a</li>

<li>b</li>

</ul>

</div>

</div>

Set an event handler in the li so that when the li is clicked, the event handler is shown.On ui, we added a new event handler. It will display click on ui when it is clicked.likewise, go to div. Because of the event bubble, when we click on the div, it displays all event handler output.

Let, const, array and object declared with const

Using let and const with variables is common in es6.When you use let, the value should change, and when you use const, the value should not.

let a = 1 ;

a=2;

console.log(a);// can be changed

const a =5 ;

a=2;

console.log(a);//error

You can modify the elements in an array or add new elements of the same type, but you can’t change the type of array element.

const a = [1,2,3,4,5]

a.push(6)// it’s ok

a = [“hello”,”bye”] // error

The same is true for objects.

What is DOM

The DOM Model is an HTML programming interface. It shows the website in order for programs to change the document structure, design and content. The DOM is the document’s node and purpose. You can then connect to languages of your website.

A web page is a document. It can be viewed as an HTML source in the browser window. It is the same document, however, in both cases. The Document Object Model (DOM) for control of the document is represented. The DOM is a webpage image that can be modified using an object-oriented script such as JavaScript.

--

--

Md Rishad Khan
0 Followers

To share my knowledge what i learn from everywhere