JavaScript program development (7) - data types of js syntax

type of data

        The data types in ECMAScript are divided into simple data types and complex data types. Simple data types are also called basic data types, including Undefined, Null, Boolean, Number and String. Complex data types include Object, Array, Date, RegExp, There are 7 types of functions, basic wrapper types and built-in objects.

Since ECMAScript is a loose data type, we cannot determine what data type a variable is through its definition. ECMAScript provides us with the typeof operator to determine the data type of a variable. Using the typeof operator on a value may return one of the following strings:

       undefined: if this value is undefined

       boolean: if the value is a boolean

       string: if this value is a string

       number: if the value is numeric 

       object: if this value is an object or null

       function: if this value is a function

The following examples are used specifically:

	function test(){
		alert("hello!");
	}
	var msg = "Hello World!";
	document.write(typeof (123));  //输出 number
	document.write(typeof ("abc")); //输出string
	document.write(typeof (test));  //输出function
	document.write(typeof false); //输出boolean
	document.write(typeof msg);

        The typeof operator can operate on variables or specific values. One thing to note is that typeof is an operator rather than a function, so the parentheses of the manipulated value after typeof are not necessary.

Undefined type

       The Undefined type has only one value: undefined. When a variable is defined with var but not initialized, the value is undefined, which has been mentioned above. There is a knowledge point here, you can take a look, but it is not necessary to know, that is, this value was not defined in the version before the third edition of ECMAScript-262, and this value was officially introduced in the third edition to distinguish the empty space. Object pointers and uninitialized variables. There is only one thing you can do with an uninitialized variable, and that is to use the typeof operator to check the data type of the variable.

       Although uninitialized variables can be automatically assigned the value of undefined, it is still recommended to display initialized variables, because when using the typeof operator to test a variable, the return value can be used to determine whether the variable is not declared or not initialized .

Null type

       In ECMAScript, there are only two data types that have only one value, Undefined is the first and Null is the second. From a logical point of view, null is a pointer to a null object, which is why, when the typeof operator is used to detect that the null type is object. If you define a new variable to save the object in the future, then it is best to assign the variable an initial value of null, so that you can use the typeof operator to detect whether the value of the variable is null, and you can know whether the variable has saved an object. quoted. As shown in the following example:

    if(dog != null){
    	alert("The object has been initialized");
    }
There is a knowledge point here, that the undefined value is derived from null, so it returns true when ECMAScript-262 specifies the operation on their equality, as shown in the following code:

	document.write(null == undefined);  //返回true
      The above mentioned that doing equality operations on null and undefined will return true, but they are not exactly the same. As mentioned above, when a variable is declared but not assigned an initial value, the variable will be automatically assigned an undefined value, but it is not necessary to display an undefined value for a newly defined variable, but this principle does not apply to null, if A variable can display an initial null value in order to hold object type data in the future but not assigned a value.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325758642&siteId=291194637