1. Type analysis :
The data types in js include undefined, boolean, number, string, object, etc. The first 4 types are primitive types, and the fifth type is reference type.
Code
var a1;
var a2 = true;
var a3 = 1;
var a4 = "Hello";
var a5 = new Object();
var a6 = null;
var a7 = NaN;
var a8 = undefined;
alert(typeof a); //Display "undefined"
alert(typeof a1); //Display "undefined"
alert(typeof a2); //Display "boolean" "
alert(typeof a3); //Display "number"
alert(typeof a4); //Display "string"
alert(typeof a5); //Display "object"
alert (typeof a6); //Display "object"
alert(typeof a7); //Display "number"
alert(typeof a8); //Display "undefined"
From the above code, we can see that undefined values and unassigned values are undefined, null is a special object, and NaN is a special number.
2. Comparison operation
var a1; //The value of a1 is undefined
var a2 = null;
var a3 = NaN;
alert(a1 == a2); //Display "true"
alert(a1 != a2); //Display "false"
alert(a1 == a3); //Display "false"
alert(a1 != a3); //Display "true"
alert( a2 == a3); //display "false"
alert(a2 != a3); //display "true"
alert(a3 == a3); //display "false"
alert (a3 != a3); //Display "true"
From the above code we can draw the conclusion: (1) undefined and null are equal; (2) NaN is not equal to any value , not equal to oneself.
JavaScript undefined attribute
Definition and usage The
undefined attribute is used to store the undefined value of JavaScript.
Syntax undefined
Explanation You cannot use for/in loop to enumerate undefined properties, nor can you use delete operation character to delete it.
undefined is not a constant and can be set to other values.
Undefined will also be returned when trying to read a non-existent object property.
Tips and Notes
< The value is equivalent to undefined The operator is used to test whether a value is undefined, because ="=" ="==">
< means None value, while>
Copy code
The code is as follows:
Output:
[Supplement] Null data type
In Jscript, the data type null has only one value: null. The keyword null cannot be used as the name of a function or variable.
A variable containing null contains "no value" or "no object". In other words, the variable does not hold a valid number, string, boolean, array, or object. You can clear the contents of a variable by assigning a null value to it.
Note that in Jscript, null is not equal to 0 (unlike in C and C). It should also be noted that the typeof operator in Jscript will report null values as type Object, not type null. This potential confusion is for backward compatibility.
Undefined data type
Undefined value is returned in the following situations:
The object property does not exist,
The variable is declared but never assigned a value.
Note that you cannot test whether a variable exists by comparing it to undefined, although you can check whether its type is "undefined". In the following code example, assume that the programmer wants to test whether the variable
// This method does not work
if (x == undefined)
// Do something
// This method also does not work - must check
// String "undefined"
if (typeof(x) == undefined)
// Do some operations
// This method is valid
if (typeof(x) == "undefined ")
// perform certain operations
Consider comparing undefined values to null.
someObject.prop == null;
The comparison result is true in the following situations,
if the property someObject.prop contains a null value,
if the property someObject.prop does not exist.
To check whether an object property exists, you can use the new in operator:
if ("prop" in someObject)
// someObject has attribute 'prop'
In JavaScript, null is the same as undefined was once confusing. The following analysis will help you understand it more clearly (or make you more confused):
- null is a keyword; undefined is a property of the Global object
- null is an object (empty object, without any Properties and methods); undefined is a value of type undefined. Try the following code:
document.writeln(typeof null); //return object
document.writeln(typeof undefined); //return undefined
- In the object model, all objects are Object Or an instance of its subclass, except for null objects:
document.writeln(null instanceof Object); //return false
- null is "equal (==)" to undefined, but not "congruent" (===)" in undefined:
document.writeln(null == undefined); //return true
document.writeln(null === undefined); //return false
- during operation Both null and undefined can be type converted to false, but are not equal to false:
document.writeln(!null, !undefined); //return true,true
document.writeln(null==false) ; //return false
document.writeln(undefined==false); //return false