DEV Community

Cover image for JavaScript Interview Questions, You Need to know.
Mohammad Sohrab Hossain
Mohammad Sohrab Hossain

Posted on

JavaScript Interview Questions, You Need to know.

What is Null vs Undefined?

Null: The intentional absence of any value in a variable is called null.
Example:
var myVar = null;
alert(myVar);

Undefined: If a parameter is not passed in a variable, it is called undefined.
Example:
let x;
console.log(x);

What is Double (==) and Triple (===)?

Double (==): Double Equals (==) checks for value equality only.
Example:
var x = "test"
var y = "test"
console.log (x == y)

Triple (===): Triple === is used for comparing two variables, but this operator also checks datatype and compares two values.
Example:
const number = 1234
const stringNumber = '1234'
console.log(number === stringNumber);

What are Hoisting, Block Scope, and Global Scope?

Hoisting: Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

Block Scope: A block scope is the area within if, switch conditions or for and while loops. Generally speaking, whenever you see {curly brackets}, it is a block.

Global Scope: the global scope is the complete JavaScript environment. In HTML, the global scope is the window object. All global variables belong to the window object.

What is Closure, Encapsulation, and Private Variable?

Closure: A closure is a combination of a function bundled together with references to its surrounding state.

Encapsulation: JavaScript Encapsulation is a process of binding the data with the functions acting on that data. It allows us to control the data and validate it.

Private Variable: A private variable is only visible to the current class. It is not accessible in the global scope or to any of its subclasses.

What is Bind, Call, Apply?

Bind: The bind () method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Call: The call () allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method.

Apply: Apply method is similar to the call method. In last this starts with an array.
Syntax: oldObject.object. apply (newObjectName, [value]);

How to find the largest element from an array?

There are the three approaches:

  1. with a FOR loop
  2. using the reduce () method
  3. using Math.max ()

Top comments (0)