JavaScript Data Type
Preface:
Variables in ECMAScipt are loose types. The so-called loose type can be used to store any type of data. Therefore, the var keyword is used in javascript to define variables. Therefore, it is impossible to determine what values the variables will store and what type the variables will actually be, because the types of variables can be changed at any time. Today, this article is just getting started with the types in javascript. Although it is very simple, I feel that data types are the prerequisite for learning a language, so you must master them.
Let's take a look at the data types in the end?
Before using the data type, we must understand the typeof OPERATOR:
The typeof operator is used to detect the Data Type of a variable. If you use typeof for values or variables, the following string will be returned.
The typeof operator can operate on variables or literal values.
1. Undefined type
The Undefined type has only one value, that is, the special Undefined. When a variable is declared with var but is not initialized, the value of this variable is Undefined.
Var box; var box1 = undefined; // if no value is assigned during the declaration, the parser automatically assigns the value undefined, so the output is true alert (box = undefined) // true alert (box1 = undefined) // true // The output is true alert (typeof box = "undefined") based on the typeof string "undefined" returned ") // true
Note: When defining variables, do not declare them as much as possible without assigning values.
2. NULL type
Null is also a data type with only one value. Its value is null. If any variable is assigned null, the Data Type of this variable is Null. The null value indicates the control object pointer. Therefore, if the declared variable is used to save the object and cannot determine which object to save at the beginning of the Declaration, it is assigned null, when using this variable, you only need to check whether the variable is null to know whether the variable saves the object.
Var box = null; alert (typeof box); // object // if the typeof operator is null, objectvar box = null is returned; if (box! = Null) {alert ('box object already exists ');} // If the defined variable is to be saved in the future, it is better to initialize the variable to null // when checking the null value, you will know whether the object reference has been allocated. Alert (undefined = null); // true // undefined is derived from null, so true is returned.
3. Boolean type
In javascript, The Boolean type is a relatively large simple data type. It has two values: true and false, because in javascript, letters are case sensitive, therefore, True and False are not Boolean values.
Assign a value to a Boolean variable as follows:
var test = true; var test1 = false;
You can call the Boolean () method to convert a value of any type to a value of the corresponding Boolean type, that is, you can convert it to true or false.
For example:
// Convert a non-empty string to true alert (Boolean ("a") // true // convert an empty string to false alert (Boolean ("")) // false // convert the object to true alert (Boolean ({a: "a"}) // true
The rules for converting values of various types into Boolean types are as follows:
4. Number Type
Javascript defines different numeric literal formats to support various numeric types
Var box = 100; // a decimal integer var box = 12.0; // The value is 0 after the decimal point and converted to 12. // the memory space required by the floating point value is twice the integer value, therefore, convert var box = 0/0; // NaNvar box = 12/0; // Infinityvar box = 12/0*0 // NaN (non-a Number) is a special value alert (NaN + 1); // NaNalert (NaN = NaN ); // false // the result of any operation with NaN is NaNalert (isNaN (NaN); // true // ECMAScript provides the isNaN () function, used to determine whether the value is NaNalert (parseInt ('456lil') // 456 returns the integer part. This function is often used in js programs to convert characters into numbers.
5. String type
String in JavaScript is a special basic data type. In many languages, String is represented as an object, but in JavaScript, String is treated as a basic data type, it is operated by passing values. But it is a special basic type.
Var strA = "this is a string"; var strB = strA; strA = "this is another string"; alert ("strB =" + strB); // strB = This is a string
As you can see from the running results, it seems that strA copied a copy to strB by passing the value. When strA changed, strB did not change. It seems that we can draw a conclusion that String is a basic data type.
Var a = "myobject";. name = "myname"; // dynamically add the name attribute alert (". name = "+. name); // access the name attribute of a. The result shows:. name = undefined
The running result shows that the String cannot be processed as an object. This also proves that although the basic type can also be added with attributes, no error is reported, but it cannot be accessed after being added. In fact, the String in javascript cannot be changed, javascript does not provide any method and syntax for changing strings.
The String type contains some special characters.
6. Object Type
Objects are actually a set of data and functions. An object can be created by executing the new operator and the name of the object type to be created. You can create a custom Object by creating an instance of the Object type and adding its attributes and (or) methods.
var o = new Object();
Each instance of an Object has the following attributes and methods:
● Constructor -- stores the function used to create the current object.
● HasOwnProperty (propertyName) -- used to check whether a given property exists in the current object instance (rather than in the instance's prototype. The parameter property name must be specified as a string (for example, o. hasOwnProperty ("name "))
● IsPrototypeOf (object) -- used to check whether the input object is a prototype of another object
● PropertyIsEnumerable (propertyName) -- used to check whether a given attribute can be enumerated using the for-in statement.
● ToString () -- returns the string representation of the object
● ValueOf () -- returns the string, value, or Boolean value of the object. It is usually the same as the return value of the toString () method.
Summary:
Undefined, Null, Boolean, Number, and String are basic data types in javascript, and objects belong to reference types. When detecting other types with typeof, a corresponding String is returned, but when detecting null or an object, "object" is returned. Boolean, Number, and String are the basic packaging types in Javascript, that is, these three are actually constructor. They are Function instances and reference types. JavaScript is one of the most troublesome languages. If you want to be familiar with JavaScript, You have to exercise more.