DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Abu Jaid
Abu Jaid

Posted on

What is this keyword in javascript?

The this keyword is probably one of the odder ducks you’ll encounter while trying to learn JavaScript. What it does, in essence, is standing in for the current object that has focus relative to the handler. Here’s an example like the one discussed in the previous section.

    <form name="myForm">
     <input type="text" name="phoneNumber" onchange="checkPhone 
     (this.value)"  />
    </form>
Enter fullscreen mode Exit fullscreen mode

this is used to suggest β€œthis input element,” and value is a property that stores the value associated with this form element. You could also access that same value as the following in order to pass along the value.

<form name="myForm">
<input type="text" name="phoneNumber" onchange="checkPhone (document.myForm.phoneNumber
The this Keyword.value)" />
</form>
Enter fullscreen mode Exit fullscreen mode

Note that the name of the form and the name of the element are both vital in getting at the correct value. That’s one reason for the this keyword; another is simple convenience

Top comments (0)

Visualizing Promises and Async/Await 🀯

async await

☝️ Check out this all-time classic DEV post