DEV Community

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)