JavaScript Interview Questions and Answers (Part-1)
1. What is JavaScript?
Answer: JavaScript is a lightweight, interpreted programming language used to make web pages interactive. It enables functionalities like dynamic updates, animations, form validations, and more.
2. What are the data types in JavaScript?
Answer: JavaScript has the following primitive data types:
- String
- Number
- Boolean
- Undefined
- Null
- BigInt
- Symbol
Objects, arrays, and functions are non-primitive data types.
3. What is the difference between “let”, “var”, and “const”?
Answer:
- var: Function-scoped, can be redeclared.
- let: Block-scoped, cannot be redeclared.
- const: Block-scoped, cannot be reassigned after declaration.
var a = 10;
let b = 20;
const c = 30;
4. What is the difference between “==” and “===”?
Answer:
- == (Loose Equality): Compares values, but does not consider data type.
- === (Strict Equality): Compares both value and data type.
console.log(5 == "5"); // true (type conversion happens)
console.log(5 === "5"); // false (type is different)
5. What are functions in JavaScript?
Answer: A function is a reusable block of code that performs a specific task.
function greet(name) {
return "Hello, " + name;
}
console.log(greet("John")); // "Hello, John"
6. What is an arrow function?
Answer: An arrow function is a shorter syntax for writing functions.
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
7. What is the difference between “null” and “undefined”?
Answer:
- null: An intentional empty value assigned to a variable.
- undefined: A variable that has been declared but not assigned any value.
let x;
console.log(x); // undefined
let y = null;
console.log(y); // null
8. What are template literals in JavaScript?
Answer: Template literals allow embedded expressions using backticks (`).
let name = "John";
console.log(`Hello, ${name}`); // "Hello, John"
9. What are JavaScript objects?
Answer: Objects store data as key-value pairs.
let person = { name: "Alice", age: 25 };
console.log(person.name); // "Alice"
10. What is an array in JavaScript?
Answer: An array is a collection of elements stored in a single variable.
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[1]); // "Banana"
11. What is the difference between “forEach()” and “map()”?
Answer:
- forEach(): Loops through elements but does not return a new array.
- map(): Loops through elements and returns a new modified array.
let nums = [1, 2, 3];
let squared = nums.map(n => n * n);
console.log(squared); // [1, 4, 9]
12. What is an event in JavaScript?
Answer: An event is an action that occurs on a webpage (e.g., click, hover, input).
document.getElementById("btn").addEventListener("click", function() {
alert("Button Clicked!");
});
13. What is the “this” keyword in JavaScript?
Answer: “this” refers to the object that is executing the function.
const person = {
name: "John",
greet() {
console.log(this.name);
}
};
person.greet(); // "John"
14. What are JavaScript loops?
Answer: Loops allow code to run multiple times.
Example of “for” loop:
for (let i = 0; i < 5; i++) {
console.log(i);
}
15. What is the difference between “slice()” and “splice()”?
Answer:
- slice(start, end): Returns a portion of an array without modifying the original.
- splice(start, count): Removes/changes elements in place.
let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(1, 3)); // [2, 3]
arr.splice(1, 2); // Removes 2 elements starting from index 1
console.log(arr); // [1, 4, 5]
16. What is JSON in JavaScript?
Answer: JSON (JavaScript Object Notation) is a format for storing and exchanging data.
let obj = { name: "John", age: 30 };
let jsonString = JSON.stringify(obj);
console.log(jsonString); // '{"name":"John","age":30}'
let jsonData = JSON.parse(jsonString);
console.log(jsonData.name); // "John"
17. What is the difference between “setTimeout()” and “setInterval()”?
Answer:
- setTimeout(): Runs a function once after a specified delay.
- setInterval(): Runs a function repeatedly at a fixed interval.
setTimeout(() => console.log("Hello after 2s"), 2000);
setInterval(() => console.log("Repeats every 3s"), 3000);
18. What is hoisting in JavaScript?
Answer: Hoisting moves function and variable declarations to the top before execution.
console.log(x); // undefined (variable hoisting)
var x = 5;
sayHello(); // "Hello" (function hoisting)
function sayHello() {
console.log("Hello");
}
19. What is localStorage and sessionStorage?
Answer:
- localStorage: Stores data with no expiration.
- sessionStorage: Stores data for the session only.
localStorage.setItem("name", "Alice");
console.log(localStorage.getItem("name")); // "Alice"
20. How do you handle errors in JavaScript?
Answer: Using try…catch.
try {
let result = x / 0;
} catch (error) {
console.log("Error:", error.message);
}
These questions cover the basics of JavaScript and will help freshers prepare for interviews! Let me know if you need more details.