Part 5–100 Advanced JavaScript Interview Questions with Answers and Code Examples
Introduction:
Welcome to the fifth installment of our series on advanced JavaScript interview questions! In this article, we’ll cover ten more challenging questions that will test your knowledge of JavaScript concepts and techniques. Each question is accompanied by a detailed answer and code examples to help you understand the topic thoroughly. Additionally, we’ll provide external links for further reading to enhance your understanding. Let’s dive in!
41- What is the purpose of the “bind()” method in JavaScript? Provide an example.
Answer: The bind()
method in JavaScript creates a new function that, when called, has its this
keyword set to a specific value. It allows us to control the context in which a function is executed. Here's an example:
const person = {
name: 'John',
sayHello: function() {
console.log(`Hello, ${this.name}!`);
}
};
const greet = person.sayHello.bind(person);
greet(); // Output: Hello, John!
Further Reading: MDN Web Docs — Function.prototype.bind()