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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more