Javascript interview questions and answers with code
To start a series for the JavaScript interview questions which may help junior to mid level developers.
Common JavaScript interview questions with examples.
1. What is let and const in JavaScript?
» Let and const are block-scoped declarations in JavaScript, used to declare variables. let allows you to reassign the value of the variable, while const creates a read-only reference to a value.
Example:
let message = "Hello, World!";
message = "Hello, JavaScript!";
console.log(message); // Output: "Hello, JavaScript!"
const PI = 3.14;
PI = 3.14159; // TypeError: Assignment to constant variable.
2. What is arrow function in JavaScript?
» Arrow functions are a shorthand for anonymous functions in JavaScript. They are also known as “fat arrow” functions.
Example:
let add = (a, b) => a + b;
console.log(add(1, 2)); // Output: 3
let numbers = [1, 2, 3, 4, 5];
let doubledNumbers = numbers.map(num => num * 2);
console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
3. What is default parameter in JavaScript?
» Default parameters allow function parameters to be initialized with default values if no values are passed or if the values are undefined.
Example:
function greet(name = "Atharv") {
return `Hello, ${name}`;
}
console.log(greet()); // Output: "Hello, "Atharv
console.log(greet("Atharv")); // Output: "Hello, Atharv"
4. What is spread operator in JavaScript?
» The spread operator allows an iterable to be expanded into its elements.
Example:
let numbers1 = [1, 2, 3];
let numbers2 = [4, 5, 6];
let numbers3 = [...numbers1, ...numbers2];
console.log(numbers3); // Output: [1, 2, 3, 4, 5, 6]
let person = { name: "Atharv", age: 30 };
let newPerson = { ...person, city: "Manipal" };
console.log(newPerson); // Output: { name: "Atharv", age: 30, city: "Manipal" }
5. What is destructuring in JavaScript?
» Destructuring is a convenient way to extract values from arrays or objects into variables.
Example:
let numbers = [1, 2, 3];
let [a, b, c] = numbers;
console.log(a, b, c); // Output: 1 2 3
let person = { name: "Atharv", age: 30 };
let { name, age } = person;
console.log(name, age); // Output: Atharv 30
6. What is a rest parameter in JavaScript?
» A rest parameter is a way to represent an indefinite number of arguments as an array. It allows a function to take a variable number of arguments. The rest parameter is represented by three dots (…).
Example:
function sum(...args) {
return args.reduce((a, b) => a + b);
}
console.log(sum(1, 2, 3, 4, 5));
// Output: 15
7. What is the difference between var, let, and const in JavaScript?
» Var is function scoped and its value can be changed, while let and const are block scoped and their values cannot be changed once they are assigned with const.
Example:
var name = "Atharv";
var name = "Gyan";
console.log(name); // Output: "Gyan"
let age = 30;
// age = 31;
console.log(age); // Output: 30
const city = "Manipal";
// city = "Manipal";
console.log(city); // Output: "Manipal"
8. What is closure in JavaScript?
» A closure is a function that has access to variables in its outer scope, even after the outer function has returned.
Example:
function outerFunction(counter) {
return function innerFunction() {
return ++counter;
};
}
let counter = 0;
let increment = outerFunction(counter);
console.log(increment()); // Output: 1
console.log(increment()); // Output: 2
9. What is a promise in JavaScript?
» A promise is an object representing the eventual completion or failure of an asynchronous operation.
Example:
let promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Hello, World!");
}, 2000);
});
promise.then(result => {
console.log(result); // Output: "Hello, World!"
});
10. What is async/await in JavaScript?
» Async/await is a way to write asynchronous code that looks and behaves like synchronous code.
Example:
async function getData() {
let response = await fetch("https://atharvgyan.com/home");
let data = await response.json();
return data;
}
getData().then(data => {
console.log(data);
});
11. What is a template literal in JavaScript?
» A template literal is a string that is delimited by backticks (`) instead of single or double quotes.
Example:
let name = "Atharv";
let message = `Hello, ${name}`;
console.log(message); // Output: "Hello, Atharv"
12. What is a class in JavaScript?
» A class is a blueprint for creating objects that defines a set of properties and methods.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
let person = new Person("Atharv", 30);
console.log(person.greet()); // Output: "Hello, my name is Atharv and I am 30 years old."
13. What is a constructor in JavaScript?
» A constructor is a special method that is called when an object is created from a class. It is used to initialize the object’s properties.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
let person = new Person("John", 30);
console.log(person.greet()); // Output: "Hello, my name is Atharv and I am 30 years old."
14. What is the difference between == and == in JavaScript?
» == Performs type coercion, meaning it converts the operands to the same type before making the comparison, while == does not perform type coercion and returns false if the operands have different types.
Example:
console.log(1 == "1"); // Output: true
console.log(1 === "1"); // Output: false
15. What is the difference between null and undefined in JavaScript?
» Undefined means a variable has been declared but has not been assigned a value, while null is explicitly set to represent no value.
Example:
let name;
console.log(name === undefined); // Output: true
let city = null;
console.log(city === null); // Output: true
16. What is the difference between let and const when declaring a constant?
» Both let and const can be used to declare a constant, but the value of a const cannot be changed after it is declared, while the value of a let can be changed.
Example:
const PI = 3.14;
// PI = 3.15;
console.log(PI); // Output: 3.14
let name = "Atharv";
name = "Atharv";
console.log(name); // Output: "Atharv"
17. What is a spread operator in JavaScript?
» The spread operator (...) allows an expression to be expanded in places where multiple elements or variables are expected.
Example:
let numbers = [1, 2, 3];
let newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // Output: [1, 2, 3, 4, 5]
let person = { name: "Atharv", age: 30 };
let newPerson = { ...person, city: "Manipal" };
console.log(newPerson); // Output: { name: "Atharv", age: 30, city: "Manipal" }
18. What is a rest parameter in JavaScript?
» A rest parameter is used to represent an indefinite number of arguments as an array. It is prefixed with ... in the function definition.
Example:
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3)); // Output: 6
19. What is a destructuring assignment in JavaScript?
» Destructuring assignment is a way to extract data from arrays or objects into separate variables.
Example:
let person = { name: "Atharv", age: 30, city: "Manipal" };
let { name, age } = person;
console.log(name); // Output: "Atharv"
console.log(age); // Output: 30
let numbers = [1, 2, 3];
let [a, b] = numbers;
console.log(a); // Output: 1
console.log(b); // Output: 2
20. What is a higher-order function in JavaScript?
» A higher-order function is a function that takes one or more functions as arguments or returns a function as a result.
Example:
function multiplyBy(factor) {
return function (number) {
return number * factor;
};
}
let double = multiplyBy(2);
console.log(double(5)); // Output: 10
Share
# Tags