Home
Daily
`this` in JavaScript
January 02, 2026
1 min

Table Of Contents

01
02
Arrow Functions
03
Event Handlers

In JavaScript, the this keyword refers to the object that is currently executing the code. Its value depends on how a function is called, not where it’s written.

Object Method

➡️ this refers to the object before the dotuser

const user = {
name: "Daniel",
sayHi() {
console.log(this.name);
}
};
user.sayHi();

Regular Function

  • In non-strict mode → window
  • In strict mode → undefined
function show() {
console.log(this);
}
show(); // window (browser) or undefined (strict mode)

Arrow Functions

  • Arrow functions don’t have their own this.
  • They use this from the surrounding scope.
const user = {
name: "Daniel",
sayHi: () => {
console.log(this.name);
}
};
user.sayHi(); // undefined

➡️ this is NOT user here

Event Handlers

button.addEventListener("click", function () {
console.log(this); // button element
});

➡️ this refers to the element that fired the event

call, apply, bind

function greet() {
console.log(this.name);
}
const user = { name: "Daniel" };
greet.call(user); // Daniel
greet.apply(user); // Daniel
const bound = greet.bind(user);
bound(); // Daniel
MethodCalls Function Now?How args are passedReturns new function?
call✅ YesOne by one❌ No
apply✅ YesArray❌ No
bind❌ NoOne by one✅ Yes

Quick Summary

SituationWhat this is
Object methodThe object
Regular functionwindow or undefined
Arrow functionInherited from outer scope
Event handler (function)The DOM element
call/apply/bindExplicitly set

Tags

#Daily

Share

Related Posts

Daily
Cookies, Sessions, and Local Storage
January 20, 2026
1 min
© 2026, All Rights Reserved.
Powered By

Social Media

githublinkedinyoutube