Home > Web Front-end > JS Tutorial > body text

What is the difference between undefined and undefined in JavaScript?

王林
Release: 2023-08-24 12:45:16
forward
1807 people have browsed it

JavaScript 中未定义和未定义有什么区别?

There are two main ways a variable can be "undefined" in JavaScript. The first is when you declare a variable without assigning a value to it. The second is when you try to access a variable that doesn't exist.

Undefined in JavaScript

  • When a variable is declared without a value, it is automatically assigned the value "Undefined". This can happen if you forget to assign a value to a variable, or intentionally don't assign a value (for example, if you are waiting for user input).

  • You will also get an "undefined" value if you try to access a variable that doesn't exist. This can happen if you type the name of a variable, or if you try to access a variable that is out of scope.

Example 1

Below is an example with explanation.

<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var foo;
      document.getElementById("result").innerHTML = foo;
   </script>
</body>
</html>
Copy after login

In the above example, the variable "foo" is declared but no value is given. So when we try to access it, "undefined" value is returned.

Example 2

<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var myObj = {};
      document.getElementById("result").innerHTML = myObj.foo;
   </script>
</body>
</html>
Copy after login

In the above example, we are trying to access the property "foo" of the object "myObj". However, since the property does not exist, an "undefined" value is returned.

Undefined in JavaScript

The "not defined" value is similar to "undefined" in that it indicates that the variable does not exist. However, there are subtle differences between the two.

"Undefined" is usually caused by a typo or an attempt to access a variable out of scope. "Undefined" is usually caused by forgetting to assign a value to a variable.

Example

The following is an example with instructions.

<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var myObj = {};
      try {
         document.getElementById("result").innerHTML = myOb.foo;
      }
      catch(err) {
         document.getElementById("result").innerHTML = err;
      }
   </script>
</body>
</html>
Copy after login

In the above example, we are trying to access the property "foo" of the object "myOb". But since the object does not exist, a ReferenceError of "myOb is not Define" is returned.

The difference between undefined and not Define

The main difference between "undefined" and "undefined" is that " "Undefined" is a value that can be assigned to a variable, while "

Another difference is that "undefined" is usually caused by forgetting to assign a value to a variable, while "undefined" is usually caused by A typo or error caused by an attempt to access a variable out of scope.

Conclusion

The conclusion is, "is not defined" and "is not defined” >" are two different values ​​in JavaScript. "Undefined" means that the variable has been declared but not assigned a value, and "undefined" means that the variable does not exist.

The above is the detailed content of What is the difference between undefined and undefined in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!