Master W3Schools JavaScript This Keyword Like a Pro

The 'this' keyword in JavaScript is a versatile tool that offers a powerful way to reference the current object. It's a reserve word that can be used in multiple ways, making it a fundamental concept to understand in JavaScript programming. Let's explore the 'this' keyword in detail, illuminating its uses, scope, and potential pitfalls.

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

'this' can behave differently depending on its context, so let's unravel its mysteries one level at a time.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Understanding the 'this' Keyword

The 'this' keyword in JavaScript refers to an object. Which object depends on how the 'this' keyword is being invoked (used/called).

β€˜this’ KeyWord in JavaScript & How it Works
β€˜this’ KeyWord in JavaScript & How it Works

'this' can point to different objects based on where it's used and how it's invoked. Let's break this down with our first sub-topic.

Lexical 'this'

HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

When you define a method on an object and call it, inside the function, 'this' refers to the object. This is called 'lexical 'this'.

For instance, if you have an object 'person' with a method 'greet', like so:

const person = {
name: 'John',
greet: function() {
console.log('Hello, ' + this.name)
}
}

'this' inside 'greet' refers to 'person'.

Javascript Loops & Iterations
Javascript Loops & Iterations

The Global 'this'

If you use 'this' in the global scope, it refers to the global object, window in the browser, or global in Node.js. This is known as the 'global 'this'.

For example, using 'this' in the browser would refer to 'window':

JavaScript: this keyword
JavaScript: this keyword

console.log(this === window); // true

Whereas in Node.js 'this' represents the 'global' object.

Invoking 'this' Changes Its Behavior

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
some type of text that is in different languages
some type of text that is in different languages
Javascript Cheet cheet
Javascript Cheet cheet
JavaScript Cheat Sheet πŸš€ | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet πŸš€ | Complete JS Reference for Beginners & Developers
javascript cheetsheet
javascript cheetsheet
πŸš€ JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
πŸš€ JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘
the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
What is JavaScript used for: 9 Reasons Why You Should Learn JavaScript
What is JavaScript used for: 9 Reasons Why You Should Learn JavaScript
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
an image of programming tricks and tips for beginners to learn in this video game
an image of programming tricks and tips for beginners to learn in this video game
Examination Apps in JavaScript with Source Code
Examination Apps in JavaScript with Source Code
the language used in web development
the language used in web development
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
Javascript String Methods
Javascript String Methods
an info sheet with different types of web pages
an info sheet with different types of web pages
a poster with the names and numbers of different types of programming languages, including an array of
a poster with the names and numbers of different types of programming languages, including an array of
different types of languages and their uses
different types of languages and their uses
what jaascrit can do to improve the language of web design and development
what jaascrit can do to improve the language of web design and development

'this' is quite dynamic and its behavior changes based on how the function is invoked, as shown in our second main topic.

Let's look at some examples to see how 'this' changes its behavior under different invocation methods.

Invoking as a Method

When a function is invoked as a method of an object, 'this' points to the object the function is invoked upon.

For example:

function greet() {
console.log('Hello, ' + this.name)
}

const person = {
name: 'John',
greet

}

person.greet(); // logs 'Hello, John'

'this' inside the function is 'person'.

Invoking with 'call()' and 'apply()'

'call()' and 'apply()' are methods that can be used to invoke a function and provide the 'this' argument.

Using 'call()' or 'apply()', you can invoke a function, treating it as a method of a specific object, thus changing the 'this' context.

For instance:

const obj1 = { name: 'Obie' };
const obj2 = { name: 'Peter' };

function greet() {
console.log('Hello, ' + this.name)
}

greet.call(obj1); // logs 'Hello, Obie'
greet.apply(obj2); // logs 'Hello, Peter'

'this' inside the function is respectively 'obj1' and 'obj2'.

The 'this' keyword is a powerful tool, but like all tools, it has its quirks and potential pitfalls. Understanding 'this' in JavaScript allows you to write more declarative code, understand and debug your code better, and write more efficient and elegant JavaScript.

Keep these points in mind, and you'll navigate the JavaScript landscape with confidence. As you continue on your JavaScript journey, keep practicing and exploring and remember, there's always more to learn!

Related Articles

W3 Null W3 Js Class String() In Js Javascript Function Return Example W3schools Border Around Text W3schools Javascript String Equals W3schools Datediff Javascript Classname Javascript W3schools W3schools Javascript Gettime Difference Between For And Foreach In Javascript