JavaScript interview questions with answers


This blog is written for students and fresh developers who prepare for JavaScript interviews. Each question is common, practical, and explained in simple words. Read it line by line. Practice the examples. You will understand what interviewers really test.

What is JavaScript

JavaScript is a programming language used to make web pages interactive. It runs in the browser and also on servers using Node.js. You use JavaScript to handle clicks, form validation, APIs, and dynamic content.

Example
A button click that shows a message uses JavaScript.

Difference between var, let, and const

var has function scope. It can be redeclared and updated. let has block scope. It can be updated but not redeclared. const has block scope. It cannot be updated or redeclared.

Use let for values that change. Use const for fixed values. Avoid var in modern code.

What is hoisting

Hoisting means JavaScript moves variable and function declarations to the top of their scope before execution.

var variables are hoisted but initialized as undefined.
let and const are hoisted but not accessible before declaration.

This is why using let and const avoids bugs.

What is a closure

A closure is created when a function remembers variables from its outer scope even after the outer function finishes.

Closures are used in data hiding, counters, and callbacks.

Difference between == and ===

== compares values only. It performs type conversion.

=== compares value and type. No conversion.

Always use === to avoid unexpected results.

What is an arrow function

Arrow functions are a shorter way to write functions. They do not have their own this. They are useful in callbacks and array methods.

What is this keyword

this refers to the object that calls the function.

In a method, this refers to the object.
In a regular function, this refers to the global object or undefined in strict mode.
In arrow functions, this is taken from the parent scope.

What is an array

An array stores multiple values in a single variable. JavaScript arrays are dynamic. Common methods include push, pop, map, filter, and reduce.

What is an object

An object stores data in key and value pairs. Objects represent real world entities like users, products, or settings.

Difference between null and undefined

undefined means a variable is declared but not assigned. null means a variable is intentionally set to no value.

What is a promise

A promise represents a value that will be available later. States of a promise are pending, fulfilled, and rejected. Promises handle asynchronous tasks like API calls.

What is async and await

async and await make asynchronous code easier to read.

async makes a function return a promise.
await pauses execution until the promise resolves.

What is an event

An event is an action like click, scroll, or key press. JavaScript listens to events and responds using event handlers.

What is event bubbling

Event bubbling means an event starts from the target element and moves up to parent elements. It is useful for event delegation.

What is DOM

DOM stands for Document Object Model. It represents the HTML structure as objects that JavaScript can manipulate.

What is event delegation

Event delegation uses a parent element to handle events for child elements.It improves performance and reduces code.

What is localStorage and sessionStorage

localStorage stores data permanently in the browser.

sessionStorage stores data for one browser session.

Both store data as key and value pairs.

Difference between synchronous and asynchronous code

Synchronous code blocks execution.

Asynchronous code runs in the background.

JavaScript uses async code to improve performance.

What is NaN

NaN means Not a Number.

It appears when a mathematical operation fails.

What is scope

Scope defines where variables can be accessed.

Types include global scope, function scope, and block scope.

Final advice for students
Understand concepts. Do not memorize answers.
Practice writing small programs.
Explain answers in simple words during interviews.
Confidence comes from clarity, not speed.

Comments

Popular posts from this blog

7 Free Resources to Learn Web Development

Important topic of js for react: