Didn't understand the "ReferenceError:'x' is not defined" error

I want to learn how to check confirm password field in form using JavaScript and in my Firefox console, I get this error “ReferenceError: check is not defined”. According to this MDN web document, I didn’t define my variable and I must admit I don’t know what that means either.

Here’s my code.
HTML:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">

</head>

<label>password :
    <input name="password" id="password" type="password" onkeyup='check();' />
</label>
<br>
<label>confirm password:
    <input type="password" name="confirm_password" id="confirm_password"  onkeyup='check();' /> 
    <span id='message'></span>
</label>

</body>
</html>

JavaScript:

 var check = function() {
      if (document.getElementById('password').value ==
          document.getElementById('confirm_password').value) {
          document.getElementById('message').style.color = 'green';
          document.getElementById('message').innerHTML = 'matching';
      } else {
      		document.getElementById('message').style.color = 'red';
          document.getElementById('message').innerHTML = 'not matching';
      }
  }

I’d like to understand how I’m keep getting these error even if I copy line by line code from different Stack Overflow answers. What am I missing here?

Thanks in advance.

Make sure you hook up javascript to your html :slight_smile:

1 Like

That’s it? I’ve tried it and to my surprise, it worked! How could it be?!

Is there any rule of thumb on when should I post JavaScript code in HTML vs. having its own file?

You can insert javascript into your html file if it’s small otherwise create a file and link it.

Glad it worked out for you!

1 Like

I see. I’ll keep that mind and thanks for the help. I appreciate it.