JavaScript typeof, null, 和 undefined
typeof operator
You can use the typeof operator to detect the data type of a variable.
The operand of typeof (of) is undefined, and the return (of) is "undefined".
The operand is a number typeof(x) = "number"
String typeof(x) = " string"
Boolean value typeof(x) = "boolean"
Object, array and null typeof(x) = "object"
Function typeof(x) = "function"
typeof The operator returns a string representing the data type of the expression.
Possible strings are: "number", "string", "boolean", "object", "function" and "undefined".
For example:
alert(typeof (123));//typeof(123) returns "number"
alert(typeof ("123"));//typeof("123") Returns "string"
typeof operator returns a string representing the data type of the expression.
php.cn
Null value (Null)
The null value type means that the variable or content has no value. For example, when there is no content in a form text input box, when we try to use JavaScript to get the value of the text input box element, the result is null.
To determine whether it is a null value, just compare the content to be compared with null:
if( x == null ) { ... }
In view of the characteristics of Web system operation, in many cases, the content of a variable can be cleared by assigning a null value to a variable without deliberately destroying the variable.
Undefined (Undefined)
After a variable is created, if a value is not assigned to the variable, the variable is an undefined type. Undefined types have a certain value undefined, so to determine whether a variable or return result is an undefined type, just compare it with undefined:
if( x == undefined ) { ... }
The difference between Undefined and Null
The Undefined type has only one value, which is undefined. When a declared variable has not been initialized, the variable's default value is undefined.
The Null type also has only one value, null. Null is used to represent an object that does not yet exist. It is often used to indicate that a function attempts to return an object that does not exist.
undefined means that a variable has not been declared, or it has been declared but has not been assigned a value (uninitialized). The value of a formal parameter variable with no actual parameters passed in is undefined. If a function returns nothing, Then the function returns undefined by default;
null is a value that means "no value";
Javascript sets the default value of unassigned variables to undefined;
Javascript never Variables will not be set to null. It is used to allow programmers to indicate that a variable declared with var has no value;
undefined is not a valid JSON, but null is; the type of undefined (typeof) is undefined;
The type of null is object.;
They are all basic types;
They are all falsy (Boolean(undefined) ->// false, Boolean(null) ->// false);
You can determine whether a variable is undefined this way. typeof variable === "undefined";
You can determine whether a variable is null this way. variable === null;
They are equal when comparing double equal signs (null==undefined ->// true), but not equal when comparing triple equal signs (null===undefined->/ / false);
php.cn