Analysis of "undefined" and "not defined" in JavaScript

Source: Internet
Author: User

varVar1;console.log (typeofVAR0);//print "Undefined", the main look under the face of var0 separate outputConsole.log (typeofVAR1);//print "undefined"Console.log (typeof true);//print "Boolean"Console.log (typeof false);//print "Boolean"Console.log (typeof1);//print "Number"Console.log (typeof"string");//print "string"Console.log (typeof(NewObject ()));//print "Object"Console.log (typeof NULL);//print "Object"Console.log (typeofNaN);//print "Number"Console.log (typeofundefined);//print "undefined" //Console.log (var0);//uncaught referenceerror:var0 is not defined The browser directly error. and terminate the program. Because Var0 never declared it. And it's not even assigned. However, you can participate in the TypeOf operation. The program does not terminate. Console.log (var1);//undefined, not it Defined. Even if it has been stated. But not assigned. This is undefined.Console.log (true);//print "true"Console.log (false);//print "false"Console.log (1);//print "1"Console.log ("string");//print "string"Console.log ((NewObject ()));//print "Object"Console.log (NULL);//print "null"; null indicates that it does not exist.Console.log (NaN);//print "NaN"; it is a concept within the number type. When two numbers are not actually numbers. That is, the non-numeric not a number. Nan. For example, 0/0. This is meaningless. So it's Nan .Console.log (undefined);//print "undefined"; indicates that the variable is declared in. But I don't know what the variable is .

I've probably explained why in the code with comments.

Change it slightly:

Console.log (VAR10); // undefined. Note that the VAR10 is declared later in this statement. Why is it not defined? Because the JavaScript language is "parse first, run later", the runtime has already completed the variable declaration, which is actually a JavaScript Code boost (hoisting) feature. Now only the Var declared variables do hoistingvar var10;

If it is not a variable. It is an attribute inside the object.

var obj1 = {};console.log (OBJ1.ATTR1); // undefined, the attributes inside the object are not. Although ATTR1 has never been declared. But the error is not defined. The engine spared on this .
function Fun1 () {}var fun1 = new Fun1 (); Console.log (FUN1.ATTR1);//undefined, here is the function. In fact, the function is also the object. New ends up being objects .

Undefined participation in the operation:

var ufo2=ufo2| | 3; // the place of operation is certainly undefined, because there is a declaration of ufo2 in front of it. But the operation is not yet assigned. So undefined is equivalent to false. Console.log (Ufo2); // 3 var Ufo4; if (Ufo4) {    Console.log ("yes");} Else {    Console.log ("no");   print no.undefined vs. false}

If it is below how the browser will error.

if (UFO3) {//Ufo3 is not defined.}

At work we often need to determine whether a variable or property is undefined. Usually use the following method: (Here is the case of the variable Age declaration)

var Age ; // Method 1console.log (typeof age = = = ' undefined '); // only the = = = operation can be used to test whether a value is undefined, because the = = operator considers the undefined value equivalent to null.  // Method 2console.log (age = = = undefined); // null means no value, and undefined represents an undeclared variable, or a variable that has been declared but not assigned a value, or an object property that does not exist. 

If you do not declare age. Mode 2 will error. That's what I said earlier. Undeclared variables (is not defined) are allowed to participate in the TypeOf operation.

The first is the most widely used and not the easiest way to make mistakes. Recommended.

Undefined cannot be enumerated using the for/in loop, nor can it be deleted with the delete operator. Undefined is not a constant, it can be set to another value. So a lot of the framework code is executed immediately when the undefined is passed as a parameter. How to avoid others to modify the original value of undefined. The individual feels that ECMA should change this to read-only. The undefined is also returned when an attempt is made to read an object property that does not exist.

 

Analysis of "undefined" and "not defined" in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.