0% found this document useful (0 votes)
3 views9 pages

JavaScript Interview Question Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

JavaScript Interview Question Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JavaScript Interview Question Answer

❖ Basic JavaScript Interview Questions

1. What is JavaScript? How is it different from Java?


JavaScript is a lightweight, interpreted scripting language primarily used to make
web pages interactive. It runs in the browser.
Difference from Java:
• Java is a statically typed, object-oriented programming language.

• JavaScript is dynamically typed and prototype-based.

2. What are the data types in JavaScript?

JavaScript has 8 data types:

• Primitive types: String, Number, Boolean, undefined, null, Symbol, BigInt

• Non-primitive: Object

3. What are variables in JavaScript? How do you declare them?

var is function-scoped and allows redeclaration.

let is block-scoped and doesn’t allow redeclaration.

const is block-scoped and used for constants; its value can’t be reassigned.
4. What is hoisting in JavaScript?
Hoisting is JavaScript's default behavior of moving declarations to the top of the
current scope.
Example:
console.log(x); // undefined

var x = 5;

5. What is the difference between == and ===?


• == checks for equality after type coercion.
• === checks for strict equality (value and type).

Example:
'5' == 5 // true
'5' === 5 // false

6. What is a function? How do you declare one?


A function is a block of code designed to perform a particular task.
Example:
function greet() {
console.log("Hello!");
}
7. What is a callback function?
A callback is a function passed into another function as an argument and executed
later.
Example:
function greet(name, callback) {
console.log("Hi " + name);
callback();
}
greet("John", () => console.log("Welcome!"));

8. Difference between function declaration and function expression?

• Function Declaration is hoisted:


• Function Expression is not hoisted:
Example:
function add(x, y) { return x + y; }
const add = function(x, y) { return x + y; };

9. What are the types of loops in JavaScript?

• for, while, do...while, for...in, for...of.


10. Difference between for, for...in, and for...of loops?

• for – loops through numbers or array indexes.


• for...in – loops through object keys.
• for...of – loops through iterable values (arrays, strings).

❖ Control Structures and Loops

11. What is the use of break and continue?

• break exits the loop.


• continue skips the current iteration and continues the loop.

12. What is an object in JavaScript?


An object is a collection of key-value pairs.
Example:
const person = { name: "Alice", age: 25 };

13. How do you create an array?

const fruits = ["apple", "banana", "mango"];


❖ Objects, Arrays, and Functions

14. What are array methods like map(), filter(), and reduce() used for?

• map() – transforms array items.


• filter() – filters based on condition.
• reduce() – reduces to a single value.

15. What is the difference between null and undefined?

• undefined: a variable has been declared but not assigned.


• null: an intentional absence of value.

16. What are arrow functions?


A shorter syntax for function expressions. They don’t have their own this.
Example:
const add = (a, b) => a + b;

17. What is the DOM?


DOM (Document Object Model) is a tree-like structure that represents the HTML
of a webpage, allowing scripts to interact with it.

18. What is event bubbling and capturing?

• Bubbling: Event propagates from the target element up.


• Capturing: Event propagates from top to target element.
❖ Miscellaneous & Common Topics

19. What is the purpose of addEventListener()?


It adds an event listener to an element.
element.addEventListener("click", function() {
alert("Clicked!");
});

20. What is the this keyword?


Refers to the object that is currently executing the function.
In a method, this refers to the object. In a regular function, it refers to the global
object (or undefined in strict mode).

21. What is a closure?


A function that remembers variables from its outer scope.

Example:
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
}
}
const counter = outer();
counter(); // 1
counter(); // 2
22. What is scope in JavaScript?
Scope determines the visibility of variables.
Types:

• Global scope
• Function scope
• Block scope (with let and const)

23. What are template literals in ES6?


A way to embed variables in strings using backticks `:

Example:
let name = "John";
console.log(`Hello, ${name}!`);

24. What are JavaScript Promises?


A promise represents the result of an asynchronous operation.

Example:
let promise = new Promise((resolve, reject) => {
resolve("Success");
});
25. What is asynchronous JavaScript?
It allows tasks to run in the background without blocking the main thread, using
callbacks, promises, or async/await.

26. Difference between synchronous and asynchronous code?

• Synchronous: Tasks execute one after the other.


• Asynchronous: Tasks execute independently; the result is handled later.

27. What are setTimeout() and setInterval()?

• setTimeout() runs a function after a delay.


• setInterval() runs it repeatedly at intervals.

Example:
setTimeout(() => console.log("Hi after 2 sec"), 2000);
setInterval(() => console.log("Repeating..."), 1000);

28. What are common JavaScript errors?

• ReferenceError: Variable not defined.


• TypeError: Wrong type of value.
• SyntaxError: Incorrect code syntax.
29. How do you handle errors in JavaScript?
Using try...catch block:

Example:

try {

// risky code
} catch (error) {
console.log("Error:", error.message);
}

30. What is event delegation?


A technique of handling events at a higher level (like a parent) instead of individual
elements.

You might also like