Js judge undefined type sample code

  • 2020-03-30 01:41:43
  • OfStack

 
if (reValue== undefined){ 
alert("undefined"); 
} 
 Found that the judgment can not come out, the last check the data to use typeof Methods:  
if (typeof(reValue) == "undefined") { 
alert("undefined"); 

} 

Typeof returns a string, with six possibilities: "number", "string", "Boolean", "object", "function", "undefined"

3.4 data type
There are five simple data types (also known as primitive data types) in ECMAScript: Undefined, Null, Boolean, Number, and String. There is also a complex data type -- Object, which is essentially an unordered set of name-value pairs. ECMAScript does not support any mechanism for creating custom types, and all values end up being one of the above six data types. At first glance, it seems that only six data types are not enough to represent all data; However, because of the dynamic nature of ECMAScript data types, there is really no need to define other data types.

3.4.1 typeof operator
Since ECMAScript is loosely typed, there needs to be a way to detect the data typeof a given variable -- typeof is the operator responsible for providing this information. Using the typeof operator on a value may return one of the following strings:  "Undefined" -- if the value is undefined; The & # 61553; "Boolean" -- if the value is Boolean; The & # 61553; "String" -- if this value is a string;

24. Chapter 3 basic concepts
The & # 61553; "Number" -- if the value is a number; The & # 61553; "Object" -- if the value is an object or null; The & # 61553; "Function" -- if this value is a function. Here are some examples of using the typeof operator:
Var message = "some string"; Alert (typeof message); / / "string" alert (typeof (the message)); / / "string" alert (typeof 95); / / "number"
TypeofExample01. HTM
These examples show that the operands of the typeof operator can be either a variable (message) or a literal value. Note that typeof is an operator, not a function, so the parentheses in the example, while usable, are not required. Sometimes the typeof operator returns confusing but technically correct values. For example, a call to typeof null returns "object" because the special value null is considered an empty object reference. Safari 5 and older, Chrome 7 and older return "function" when the typeof operator is called on a regular expression, whereas other browsers return "object" in this case.

Technically, functions in ECMAScript are objects, not data types. However, functions do have some special properties, so it is necessary to distinguish functions from other objects by the typeof operator.
 
function test1(){ 
var message; 
if(typeof(message)=="undefined") 
alert(" The variable value is undefined "); 
else 
alert(message); 
} 
var cc=test1; 
cc(); 

Related articles: