Introductions
The this keyword is an important part of JavaScript because its value depends on how a function is called. The methods call(), apply(), and bind() allow you to control the value of this. These concepts can feel confusing at first, so this chapter starts with simple examples and gradually moves toward practical usage. JavaScript this, call(), apply() and bind() practice questions with solutions help to build concepts.
Question 1: Understand this Inside an Object
Problem
Create a student object with a name property and a greet() method. Use this to access the student’s name.
Solution
const student = {
name: "Rahul",
greet: function() {
console.log("Hello, " + this.name);
}
};
student.greet();
Output
Hello, Rahul
Step-by-step Explanation
- The
studentobject has anameproperty. - The
greet()method belongs to the object. - Inside
greet(),thisrefers to the object that called the method. this.nametherefore accesses"Rahul".- The greeting is displayed.
Question 2: Use this to Access Multiple Object Properties
Problem
Create a person object with name and age. Create a method that displays both values using this.
Solution
const person = {
name: "Aman",
age: 20,
showDetails: function() {
console.log("Name:", this.name);
console.log("Age:", this.age);
}
};
person.showDetails();
Output
Name: Aman
Age: 20
Step-by-step Explanation
nameandageare properties ofperson.showDetails()is a method of the same object.this.nameaccesses thenameproperty.this.ageaccesses theageproperty.- Both values are displayed.
Question 3: Use call() to Borrow a Method
Problem
Create two objects with different names. Create a greet() method in the first object and use call() to execute that method for the second object.
Solution
const person1 = {
name: "Rahul",
greet: function() {
console.log("Hello, " + this.name);
}
};
const person2 = {
name: "Priya"
};
person1.greet.call(person2);
Output
Hello, Priya
Step-by-step Explanation
person1has thegreet()method.person2has only anameproperty.- Normally,
person1.greet()usesperson1asthis. call(person2)tells JavaScript to executegreet()withperson2asthis.- Therefore,
this.namebecomes"Priya".
Question 4: Pass Arguments Using call()
Problem
Create a function that uses this.name and accepts a city as an argument. Use call() to provide the object and city.
Solution
function introduce(city) {
console.log("My name is " + this.name);
console.log("I live in " + city);
}
const person = {
name: "Neha"
};
introduce.call(person, "Delhi");
Output
My name is Neha
I live in Delhi
Step-by-step Explanation
introduce()usesthis.name.personcontains thenameproperty.call()setsthistoperson."Delhi"is passed as the function argument.this.namebecomes"Neha".citybecomes"Delhi".
The syntax is:
functionName.call(object, argument1, argument2);
Question 5: Use apply() with Function Arguments
Problem
Create a function that adds two numbers. Use apply() to provide the arguments as an array.
Solution
function add(a, b) {
return a + b;
}
const numbers = [10, 20];
const result = add.apply(null, numbers);
console.log(result);
Output
30
Step-by-step Explanation
add()expects two arguments.- The
numbersarray contains10and20. apply()accepts the arguments as an array.- The values are supplied to
add(). - The function calculates
10 + 20. - The result is
30.
A useful way to remember the difference:
call() → arguments separately
apply() → arguments as an array
Question 6: Use apply() with this
Problem
Create a function that displays a person’s name and age. Use apply() to set this and provide the age as an argument.
Solution
function showDetails(age) {
console.log("Name:", this.name);
console.log("Age:", age);
}
const person = {
name: "Aman"
};
showDetails.apply(person, [21]);
Output
Name: Aman
Age: 21
Step-by-step Explanation
showDetails()usesthis.name.personcontains the name"Aman".apply()setsthistoperson.[21]provides the function argument.agereceives21.- Both values are displayed.
Question 7: Create a Permanent Function with bind()
Problem
Create a function that displays a person’s name. Use bind() to permanently associate the function with a specific object.
Solution
function greet() {
console.log("Hello, " + this.name);
}
const person = {
name: "Rahul"
};
const greetPerson = greet.bind(person);
greetPerson();
Output
Hello, Rahul
Step-by-step Explanation
greet()usesthis.name.personcontains the requiredname.bind(person)creates a new function.- The new function remembers
personas itsthisvalue. - Calling
greetPerson()displays"Hello, Rahul".
Unlike call() and apply(), bind() does not immediately execute the function.
Question 8: Use bind() with Arguments
Problem
Create a function that displays a person’s name and city. Use bind() to set both the this value and the city.
Solution
function introduce(city) {
console.log(this.name + " lives in " + city);
}
const person = {
name: "Priya"
};
const introducePriya = introduce.bind(person, "Delhi");
introducePriya();
Output
Priya lives in Delhi
Step-by-step Explanation
introduce()usesthis.name.personprovides the name.bind(person, "Delhi")setsthistoperson.- It also prepares
"Delhi"as the first argument. introducePriya()is called later.- The function displays the prepared information.
Question 9: Compare call(), apply() and bind()
Problem
Create a function that displays a person’s name. Use call(), apply(), and bind() with the same object.
Solution
function greet() {
console.log("Hello, " + this.name);
}
const person = {
name: "Neha"
};
// call()
greet.call(person);
// apply()
greet.apply(person);
// bind()
const boundGreet = greet.bind(person);
boundGreet();
Output
Hello, Neha
Hello, Neha
Hello, Neha
Step-by-step Explanation
All three methods can control the value of this, but they behave differently.
call()
greet.call(person);
Calls the function immediately.
apply()
greet.apply(person);
Also calls the function immediately. It is especially useful when function arguments are supplied as an array.
bind()
const boundGreet = greet.bind(person);
Creates a new function that can be called later.
A simple comparison:
| Method | Executes immediately? | Arguments |
|---|---|---|
call() | Yes | Separate arguments |
apply() | Yes | Array |
bind() | No | Creates a new function |
Question 10: Use call() with Two Objects
Problem
Create a function that displays a person’s name and profession. Use the same function with two different objects using call().
Solution
function showProfile() {
console.log(this.name + " is a " + this.profession);
}
const person1 = {
name: "Rahul",
profession: "Developer"
};
const person2 = {
name: "Priya",
profession: "Designer"
};
showProfile.call(person1);
showProfile.call(person2);
Output
Rahul is a Developer
Priya is a Designer
Step-by-step Explanation
showProfile()usesthis.nameandthis.profession.person1contains Rahul’s information.call(person1)makesthisrefer toperson1.- The first profile is displayed.
call(person2)changesthistoperson2.- The second profile is displayed.
- One function can therefore be reused with different objects.
Key Takeaways
thisrefers to a value determined by how a function is called.- Inside an object method,
thiscommonly refers to the object calling the method. call()lets you explicitly setthisand executes the function immediately.apply()also setsthisand executes immediately.apply()accepts function arguments as an array.call()accepts arguments individually.bind()creates a new function with a specifiedthisvalue.bind()does not execute the function immediately.call(),apply(), andbind()are useful for reusing functions with different objects.- These methods are important when working with object-oriented and reusable JavaScript code.
FAQs
1. What is this in JavaScript?
this is a special keyword whose value depends on the way a function is called.
For example:
const user = {
name: "Rahul",
greet: function() {
console.log(this.name);
}
};
user.greet();
Here, this refers to the user object.
2. What does call() do in JavaScript?
call() invokes a function immediately while allowing you to specify its this value.
function greet() {
console.log(this.name);
}
const user = {
name: "Aman"
};
greet.call(user);
3. What does apply() do in JavaScript?
apply() invokes a function immediately and allows you to specify its this value. Its arguments are supplied as an array.
function add(a, b) {
return a + b;
}
console.log(add.apply(null, [10, 20]));
4. What does bind() do in JavaScript?
bind() creates a new function with a specified this value.
const user = {
name: "Neha"
};
function greet() {
console.log(this.name);
}
const newGreet = greet.bind(user);
newGreet();
5. What is the difference between call() and apply()?
Both execute the function immediately and can set this.
The main difference is how arguments are supplied.
add.call(null, 10, 20);
With apply():
add.apply(null, [10, 20]);
6. What is the difference between bind() and call()?
call() executes the function immediately.
greet.call(user);
bind() creates a new function that can be executed later.
const newGreet = greet.bind(user);
newGreet();
7. Why are call(), apply() and bind() useful?
They allow you to control the this value and reuse functions with different objects. They are particularly useful when working with reusable functions and object-based JavaScript code.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
