DEV Community

Ahmed Niazy
Ahmed Niazy

Posted on

JavaScript Interview Questions and Answers

JavaScript Basics
What is the difference between == and ===?
== compares values with type coercion, while === compares values and types strictly.

What are primitive data types in JavaScript?
String, Number, Boolean, Null, Undefined, Symbol, and BigInt.

Difference between var, let, and const?

var is function-scoped and hoisted.

let and const are block-scoped.

const cannot be reassigned.

What is hoisting in JavaScript?
It’s JavaScript’s default behavior of moving declarations to the top of the scope.

Difference between undefined and null?
undefined means a variable is declared but not assigned; null is an intentional absence of value.

What is scope in JavaScript?
Scope defines the access level of variables (global, function, or block scope).

What is a closure?
A closure is a function that remembers its outer scope even after the outer function has returned.

Function declaration vs. function expression?
Declarations are hoisted; expressions are not.

What are declared, undefined, and undeclared variables?

Declared: properly defined with var, let, or const.

Undefined: declared but no value assigned.

Undeclared: not declared at all.

What is a callback function?
A function passed as an argument to another function to be executed later.

Advanced Concepts
What is the Event Loop?
It’s the mechanism that handles asynchronous callbacks in JavaScript.

Synchronous vs. Asynchronous in JavaScript?

Synchronous: tasks run sequentially, blocking execution.

Asynchronous: tasks run in the background without blocking.

What are Promises?
Objects representing the eventual completion or failure of an asynchronous operation.

What is async/await?
A syntax for writing asynchronous code that looks synchronous, built on Promises.

What is the Prototype Chain?
A mechanism for inheritance where objects inherit from other objects via proto.

Shallow Copy vs. Deep Copy?

Shallow Copy copies only the first level.

Deep Copy recursively copies all nested levels.

What is IIFE (Immediately Invoked Function Expression)?
A function that runs immediately after being defined, often used to avoid polluting the global scope.

How does garbage collection work in JavaScript?
JavaScript automatically removes objects that are no longer referenced to free memory.

What is this in JavaScript?
It refers to the object from which a function is called, depending on the context.

Arrow Function vs. Regular Function?
Arrow functions don’t have their own this and cannot be used as constructors.

ES6+ Features
What is destructuring?
A syntax that allows unpacking values from arrays or properties from objects into variables.

What is the difference between spread and rest operators?

Spread ... expands elements.

Rest ... collects elements into an array.

Map vs. Object?

Map allows any type of key and maintains insertion order.

Object keys are strings or symbols only.

Set vs. Array?

Set stores unique values.

Array can have duplicate values.

How are classes defined in modern JavaScript?
Using the class keyword introduced in ES6 for cleaner, object-oriented syntax.

MORE

Link1
Link2
Link3
Link4

Top comments (0)