JavaScript Notes
Master JavaScript call() method: this binding, syntax, constructor inheritance, function borrowing, strict mode differences, and top interview Q&A.
What is call()?
call() is a method available on every JavaScript function via Function.prototype. It lets you immediately invoke a function while explicitly setting what this should be — and you pass arguments one by one as individual values.
One-liner:call()runs a function right now, with you choosing whatthismeans, passing each argument separately.
Syntax
functionName.call(thisArg, arg1, arg2, ...argN);| Parameter | Required | Description |
|---|---|---|
thisArg | Yes | Value that becomes this inside the function |
arg1, arg2, ... | No | Arguments passed individually to the function |
Return value: The result returned by the function — same as calling it normally.
How call() Works Internally
Basic Example — Step by Step
function show(city) {
return `${this.name} from ${city}`;
}
const user = { name: "Ravi" };
console.log(show.call(user, "Delhi"));Ravi from Delhi
Execution trace:
Execution Context Stack Diagram
Multiple Examples
Example 1 — Function Borrowing
Bruno says Woof Whiskers says ...
cat doesn't have speak(), but call() lets it temporarily use it with this = cat.
Example 2 — Constructor Inheritance
The most important real-world use of call():
function Vehicle(type, speed) {
this.type = type;
this.speed = speed;
}
function Car(type, speed, brand) {
Vehicle.call(this, type, speed); // Inherit Vehicle's initialization
this.brand = brand;
}
const myCar = new Car("Sedan", 180, "Toyota");
console.log(myCar.type);
console.log(myCar.speed);
console.log(myCar.brand);Sedan 180 Toyota
Vehicle.call(this, ...) runs Vehicle's constructor body with the new Car instance as this, so all of Vehicle's assignments happen on the Car instance.
Example 3 — Type Checking with call()
function getType(value) {
return Object.prototype.toString.call(value);
}
console.log(getType([]));
console.log(getType({}));
console.log(getType(null));
console.log(getType(42));
console.log(getType("hello"));[object Array] [object Object] [object Null] [object Number] [object String]
This is a famous JavaScript pattern — typeof can't distinguish arrays from objects, but Object.prototype.toString.call(value) can.
Example 4 — call() with Multiple Arguments
function createProfile(city, role, experience) {
return {
name: this.name,
city,
role,
experience,
};
}
const person = { name: "Ananya" };
const profile = createProfile.call(person, "Bengaluru", "Full Stack Dev", "3 years");
console.log(profile);{ name: "Ananya", city: "Bengaluru", role: "Full Stack Dev", experience: "3 years" }Strict Mode vs Non-Strict Mode
The behavior of call() when null or undefined is passed as thisArg differs:
// NON-STRICT MODE
function checkThis() {
return this;
}
console.log(checkThis.call(null)); // global object (window / global)
console.log(checkThis.call(undefined)); // global object
console.log(checkThis.call(42)); // Number { 42 } (boxed)[object global] [object global] [object Number: 42]
// STRICT MODE
"use strict";
function checkThisStrict() {
return this;
}
console.log(checkThisStrict.call(null)); // null (exact value)
console.log(checkThisStrict.call(undefined)); // undefined
console.log(checkThisStrict.call(42)); // 42 (no boxing)null undefined 42
Common Mistakes
❌ Mistake 1 — Passing Array to call() Instead of apply()
1,2,3undefinedundefined
// CORRECT — use spread or apply()
console.log(sum.call(null, ...nums));
console.log(sum.apply(null, nums));6 6
❌ Mistake 2 — Using call() on Arrow Functions for this
this.name = undefined
Arrow functions ignore the thisArg in call(). Their this is fixed lexically.
❌ Mistake 3 — Losing this When Extracting Methods
NaN
// FIX with call
console.log(counter.increment.call(counter));1
call() vs apply() vs bind() — Quick Comparison
call() | apply() | bind() | |
|---|---|---|---|
| - | ---------- | ----------- | ---------- |
| Runs now? | ✅ | ✅ | ❌ |
| Args format | Individual | Array | Individual (preset) |
| Returns | Result | Result | New function |
Permanent this? | ❌ | ❌ | ✅ |
Interview Questions 🎯
Q1. What is the call() method in JavaScript?
call() is a Function.prototype method that immediately invokes a function with an explicitly specified this value and arguments passed individually. It lets you control what this means inside any regular function.
Q2. What is the difference between call() and apply()?
The only difference is argument format:
call(thisArg, a, b, c)— arguments passed individuallyapply(thisArg, [a, b, c])— arguments passed as an array
Both execute the function immediately and return the same result.
Q3. Does call() permanently change this?
No. call() changes this only for that one invocation. The original function is unchanged — the next call uses default this resolution.
Q4. What happens with call() in strict mode vs non-strict mode?
- Non-strict:
null/undefinedthisArg → global object; primitives get boxed (wrapped in objects) - Strict:
thisis exactly what you passed —nullstaysnull,42stays42, no boxing
Q5. How is call() used in constructor inheritance?
function Animal(name) { this.name = name; }
function Dog(name, breed) {
Animal.call(this, name); // Run Animal's setup on Dog's `this`
this.breed = breed;
}
const d = new Dog("Rex", "Lab");
// d.name = "Rex", d.breed = "Lab"Animal.call(this, name) runs Animal's initialization code with the new Dog instance as this, effectively inheriting properties.
Q6. Why does Object.prototype.toString.call(value) work for type checking?
toString() on Object.prototype returns a reliable type tag like [object Array]. But if you just call [].toString(), it uses Array's overridden toString. By using .call(value), you force Object.prototype.toString to run with this = value, getting the reliable result every time.
Q7. What is function borrowing with call()?
Function borrowing means using one object's method on a different object using call():
const a = { name: "Alice" };
const b = { name: "Bob", greet() { return `Hi, I am ${this.name}`; } };
console.log(b.greet.call(a)); // Alice borrows greet from bHi, I am Alice
Q8. Can call() be used to invoke arrow functions with different this?
call() can invoke arrow functions, but the thisArg is completely ignored. Arrow functions use the lexically captured this from their definition scope, which cannot be overridden.
Key Takeaways 🔑
call()immediately invokes a function with a customthis- Arguments are passed individually (vs.
apply()'s array format) - The
thisbinding is temporary — only for that one call - In non-strict mode, primitive
thisArgvalues get boxed; in strict mode, they don't - Arrow functions ignore the
thisArgpassed tocall() - Major use cases: constructor inheritance, type checking, function borrowing
call()does not permanently modify the function — it's a clean, temporary invocation
Practice Problems
- Write a
describeCar(color, year)function andcall()it with a car object asthis. - Use
call()to implement constructor inheritance fromShapeintoCircle. - Use
Object.prototype.toString.call()to detect the types of 5 different values. - Extract a method from an object and use
call()to run it with the original object. - Compare the behavior of
call(null, ...)in strict vs non-strict mode with a code example.
Summary
call() is a Function.prototype method that immediately runs a function with a custom this value and individually passed arguments. The this change is temporary — only for that invocation. It is essential for constructor inheritance (Parent.call(this, ...)), type-safe detection (Object.prototype.toString.call(val)), and function borrowing. In modern code, spread syntax often reduces the need to choose between call() and apply() — simply use fn.call(ctx, ...argsArray) to get the best of both.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript call() Method - Complete Guide with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, advanced, call, javascript call() method - complete guide with examples
Related JavaScript Master Course Topics