The this Keyword (call, apply & bind)
this is the single most misunderstood word in JavaScript — and once it clicks, half of the "weird" bugs you've hit suddenly make sense. The trick: this is not decided by where a function is written , but by how it is called .
Learn The this Keyword (call, apply & bind) in our free JavaScript course — a beginner-friendly interactive lesson with runnable examples, a practice…
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll learn the four call patterns that set this , why callbacks "lose" it, and how call , apply , and bind let you take back control.
📚 Prerequisites: You should be comfortable with functions and basic objects. If you know that objects hold key/value pairs and that methods are just functions stored on an object, you're ready.
💡 Running Code Locally: While this online editor runs real JavaScript, some advanced examples may have limitations. For the best experience:
🗣️ Real-World Analogy: Think of the word "my" in a spoken sentence:
Most beginners assume this refers to the object the function "belongs to". It doesn't. A function has no fixed this at all. The value is decided fresh every time the function is called , based on how the call is written. Look at the object immediately to the left of the dot at the moment of the call — that's usually your this .
Every function call falls into one of these four patterns. Learn them once and you'll never guess again:
This is the bug that bites everyone. You have a method that works perfectly when you call it directly, but the moment you hand it to setTimeout , an event listener, or map , this vanishes. Why? Because those callers invoke your function as a plain function — there's no object before the dot anymore.
Fill in the blank so the method reads its own object's data.
fn.call(thisArg, arg1, arg2, ...) runs fn immediately, sets this to thisArg , and passes the rest as normal arguments. It's perfect for borrowing a method from one object to use on another.
fn.apply(thisArg, [args]) is identical to call except the arguments arrive as a single array. Remember it as "A for Array" . It shines when you already have your values in an array and the function expects them as separate parameters.
Unlike call and apply , bind does not run the function. It returns a new function whose this is permanently fixed. That makes it the go-to fix for event handlers and timers, where the framework decides when (and how) your function gets called. You can also pre-fill leading arguments — a technique called partial application .
Create a bound copy of greet that is locked to the captain object.
These lines are scrambled. Put them in the right order so the bound function logs Hi Nova .
Why: you must declare the function and the object before you can bind them, and bind before you call.
Predict the output before revealing the answer.
7 — called as o.get() , so this is o . (Calling f() instead would lose this .)
15 — this.a is locked to 10 , and 5 is passed as b .
x9 — call invokes immediately with this set to the object we passed.
Up next: Destructuring — unpack arrays and objects in a single, elegant line. 📦
Practice quiz
According to the golden rule of this lesson, what decides the value of inside a regular function?
- Where the function is written/defined
- How the function is called (the call site)
- The name of the function
- The file the function lives in
Answer: How the function is called (the call site). this is decided fresh every time, based on how the call is written — the call site, not where the function is defined.
Given , what does log?
- undefined
- 0
- 7
- It throws an error
Answer: 7. Called as o.get(), the object before the dot is o, so this is o and this.n is 7.
In strict mode (modules and classes), what is inside a plain function call like with no object before the dot?
- The global/window object
- undefined
- An empty object {}
- The function itself
Answer: undefined. A plain function call has no receiver, so in strict mode this is undefined (it would be the global object in non-strict mode).
What is inside a constructor call such as ?
- undefined
- The global object
- The brand-new object being created
- The Dog function
Answer: The brand-new object being created. A constructor call with new binds this to the freshly created instance.
Why do arrow functions help fix the 'lost this' problem inside a method's callback?
- They create their own new this each call
- They capture this lexically from the enclosing scope and have no own this
- They always set this to the global object
- They run faster than regular functions
Answer: They capture this lexically from the enclosing scope and have no own this. Arrow functions have no own this; they inherit this from the surrounding scope where they were created.
What does return, given ?
- Lena works as a designer
- undefined works as a designer
- Lena works as a role
- It throws an error
Answer: Lena works as a designer. call invokes immediately with this set to {name:'Lena'} and passes 'designer' as the role argument.
Which statement about versus is correct?
- call takes arguments as an array; apply one by one
- Both take arguments only as an array
- call takes arguments one by one; apply takes them as a single array
- apply does not run the function, it returns a new one
Answer: call takes arguments one by one; apply takes them as a single array. Both run the function immediately; call takes args one by one, apply takes them as one array ('A for Array').
What does evaluate to?
- 5
- 130
- 199
- NaN
Answer: 130. apply spreads the array into separate arguments for Math.max, so it returns the largest value, 130.
How is different from and ?
- bind runs the function immediately
- bind returns a new function with this permanently fixed, to call later
- bind only works on arrow functions
- bind changes this every time it is called
Answer: bind returns a new function with this permanently fixed, to call later. bind does not run the function; it returns a new function whose this is locked in for later calls.
Given then , what does log if widget.label is 'Save'?
Once a function is bound, this can never be re-bound — the first bind wins, so it stays [Save].