JavaScript interview questions
give me most asked question in the interview of the javascript for campus placement and with
answer also.total 50 questions
Here are 50 most asked JavaScript interview questions with answers (important for campus
placements).
The answers are in simple, easy-to-understand language with examples where required.
Top 50 JavaScript Interview Questions and Answers
1. What is JavaScript?
JavaScript is a scripting language used to make web pages interactive. It runs in the browser and can
manipulate HTML and CSS.
2. Difference between var, let, and const?
var: Function-scoped, can be redeclared and updated.
let: Block-scoped, can be updated but not redeclared.
const: Block-scoped, cannot be updated or redeclared.
3. What are data types in JavaScript?
Primitive: string, number, boolean, null, undefined, symbol, bigint.
Non-primitive: objects (arrays, functions).
4. What is hoisting?
Hoisting is JavaScript’s default behavior of moving declarations to the top of the scope before code
execution.
Example:
javascript
console.log(a); // undefined
var a = 10;
5. What is the difference between == and ===?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/8
== checks only values (performs type conversion).
=== checks both value and type (strict).
6. Explain NaN in JavaScript.
NaN stands for "Not a Number". It means the result is not a valid number.
javascript
"abc" / 2; // NaN
7. What are arrow functions?
Arrow functions provide a shorter syntax:
javascript
const add = (a,b) => a+b;
8. What is a callback function?
A callback is a function passed as an argument to another function, executed later.
javascript
function greet(name, callback){
console.log("Hi " + name);
callback();
}
greet("Raj", () => console.log("Welcome!"));
9. Explain closures.
Closure is when an inner function remembers variables from its outer function even after the outer
function has finished.
javascript
function outer(){
let x = 5;
return function inner(){ console.log(x); }
}
let fn = outer();
fn(); // 5
10. What is an event loop?
Event loop allows JavaScript to handle asynchronous operations. It picks tasks from the queue and
executes them after the call stack is empty.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/8
11. Difference between synchronous and asynchronous?
Synchronous: Executes line by line.
Asynchronous: Does not wait; tasks like setTimeout and promises are handled separately.
12. What is the difference between null and undefined?
null: Intentional absence of value.
undefined: Variable declared but not assigned.
13. What are template literals?
String enclosed with backticks allowing variable embedding:
javascript
let name = "John";
console.log(`Hello ${name}`);
14. What is a promise?
A promise represents a value that will be available now, later, or never.
javascript
let p = new Promise((resolve,reject)=>{
resolve("done");
});
15. Explain async/await.
async/await is syntax to handle promises more cleanly.
javascript
async function fetchData(){
let data = await fetch('url');
}
16. What are default parameters?
If no value is provided, the default is used.
javascript
function greet(name="Guest"){ console.log(name); }
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/8
17. What is an IIFE?
Immediately Invoked Function Expression:
javascript
(function(){
console.log("Runs instantly");
})();
18. What are pure functions?
A function that gives the same output for the same input and has no side effects.
19. Explain higher-order functions.
Functions that take other functions as arguments or return a function.
javascript
[1,2,3].map(x => x*2);
20. What is this keyword?
this refers to the object that owns the function.
In global scope, this refers to window in browsers.
21. Difference between arrow functions and normal functions regarding this.
Arrow functions do not have their own this. They inherit from the surrounding scope.
22. Explain setTimeout and setInterval.
setTimeout: Runs a function after a delay.
setInterval: Runs repeatedly after a fixed time.
23. What is DOM?
Document Object Model – it represents the page structure as objects.
24. How do you select elements in DOM?
getElementById
querySelector
querySelectorAll
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/8
25. Explain event bubbling and event capturing.
Bubbling: Event goes from inner to outer.
Capturing: Event goes from outer to inner.
26. Difference between localStorage, sessionStorage, and cookies.
localStorage: Permanent storage in browser.
sessionStorage: Temporary storage (until tab closes).
cookies: Small data sent with every request.
27. Explain JSON.
JSON is a text format for storing and transferring data.
javascript
JSON.stringify(obj); // Object to string
JSON.parse(str); // String to object
28. What is the difference between var and function scope?
var is function-scoped, while let and const are block-scoped.
29. Explain destructuring.
Extracting values from arrays/objects easily:
javascript
let [a,b] = [1,2];
let {x,y} = {x:10,y:20};
30. What are rest and spread operators?
Rest: Combines arguments into an array.
Spread: Expands array/object into individual elements.
31. What are JavaScript objects?
Objects are collections of key-value pairs.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/8
javascript
let person = {name:"Raj", age:20};
32. How do you check type in JavaScript?
Using typeof:
javascript
typeof "Hello"; // string
33. What are array methods like map, filter, reduce?
map: Creates new array by transforming each element.
filter: Returns elements that satisfy a condition.
reduce: Reduces array to a single value.
34. Explain for…of and for…in.
for…of: Iterates over values.
for…in: Iterates over keys (object properties).
35. What is the difference between == undefined and typeof var === "undefined"?
typeof safely checks if a variable is undefined (even if not declared).
36. Explain event delegation.
Instead of adding event to many elements, add to a parent and handle through event.target.
37. What are modules in JavaScript?
Modules allow splitting code into files using export and import.
38. What is a debounce function?
Delays execution until a certain time has passed without new events.
39. What is throttling?
Ensures a function runs at most once in a fixed time interval.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/8
40. Explain strict mode.
"use strict"; helps write cleaner code by throwing errors for bad practices.
41. Explain typeof null.
It returns object (this is a historical JavaScript bug).
42. Explain memory leak in JavaScript.
When memory that is no longer needed is not released (e.g., unused event listeners).
43. What is a polyfill?
A polyfill is a piece of code that adds a feature that the browser may not support.
44. Explain symbol type.
Symbol is a unique and immutable primitive value used as object keys.
45. What is BigInt?
BigInt can store very large integers beyond Number.MAX_SAFE_INTEGER.
46. Explain shallow copy vs deep copy.
Shallow copy: Copies only references.
Deep copy: Copies nested objects as well.
47. What is the difference between splice and slice?
slice: Returns a part of an array (no change).
splice: Removes or adds elements (changes original).
48. What are generators?
Functions with function* that can pause execution using yield.
49. What are default exports and named exports?
Default export: Only one per file.
Named export: Multiple exports.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/8
50. Why JavaScript is single-threaded?
JavaScript runs on a single thread using an event loop for concurrency to avoid race conditions.
Would you like me to:
1. Give these 50 questions in PDF format?
2. Or provide code-based examples for top 20 questions?
3. Or provide MCQ (multiple-choice) version for campus placement?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/8