Each value in JavaScript belongs to a data type, and JavaScript has 6 types of data. These 6 data types fall into two categories: the original type (primitive type) and the object type, which consists of 3 data types:
Numeric (number) Boolean string (String)
The object type consists of 3 data types:
Object Array (function)
In addition to the 6 data types above, JavaScript also defines two additional special primitive values: null (empty) and undefined (undefined) , which are not numeric, string, and Boolean values, They typically represent unique members of their particular type. generic JavaScript objects are unordered collections of named values. But arrays and functions are also special objects: an array is an ordered collection object with a numbered value; function is an object with executable code associated with it using typeof () method determines what type a value belongs to:
1 typeof // "Number" 2 typeof // "string" 3 typeof true // "Boolean"
In addition to the three basic types, test with the typeof () method, the function will return "function" and undefined will return undefined :
1 var function () {}2typeof// "function"3typeof// "undefined"
With this feature, we can check whether a variable is defined in the program:
if (typeof o = = "undefined") {//referenceerror:ois not defined}
In addition to the return type in 5 above, all other data types return an object :
1 typeof null; // "Object" 2 typeof {}; // "Object" 3 typeof []; // "Object"
In order to be able to display the distinguished objects and arrays, we need to use another operator, instanceof :
1 var o = {} 2 o Object; // true 3 o instanceof Array; // false 4 var a = 5 a instanceof Object; // false 6 a instanceof Array; // true
null and undefined also describe "null", but undefined can be understood as a deeper "empty value". Null usually means that a variable has been defined, but it is not given an initial value, and in many cases it is actually defined as:
var null; // indicates that an empty variable, str, is defined first for use by subsequent programs
wrapper objects in JavaScript in JavaScript, an object is a composite value that is a property and a named collection of merit. Through the "." Symbols to reference property values and methods, but we see that the string also has the same properties and methods:
var s = "Hello World"; var word = s.substring (S.indexof ("") +1,s.length); // using properties of strings
In fact, as long as the properties of the string s are referenced, JavaScript converts the string to an object by calling the new string (s) , which inherits the string's method and is used to manipulate properties and references. Once the attribute reference is finished, the newly created object is destroyed. As with strings, arrays and Booleans have their own methods: the number () and Boolean () constructors are used to create a temporary object. Null and undefined do not have a temporary wrapper object, and accessing their properties returns a type error.
var s = "Test"= 4; var t = len;
When the above code is run, the value of T returns undefined because the variable len defined in the second row is destroyed after the definition. This code shows that when reading the properties of strings, numbers, and booleans, it behaves like a normal object. However, when you try to assign a value to it, it is ignored. The temporary object created when accessing a string, numeric, or Boolean property is called a wrapper object, and the most obvious difference from a normal object is that the properties of the wrapper object are read-only. It is also possible to create wrapper objects explicitly through constructors:
var s = "Test", n = 1,b=true ; // var S = new String (s); // var n = new number (n); // A numeric object var b = new Boolean (b); // A Boolean object
using "= =" to detect s,n,b and S,n,b will return true, and the typeof() operator can see the difference between the original value and its wrapper object. Primitive values are fundamentally different from objects in JavaScript, and the original values are immutable. For a string, a method that uses a string seems to change its value, actually returning a new string. For example:
var s = "Hello"; // defines a string s.touppercase () consisting of lowercase letters ; // returns "HELLO", but does not change the value s of S ; // "Hello", the original value has not changed
The original values are compared only when their values are equal, such as two separate string comparisons, only if their lengths are equal and the characters of each index are equal. For objects, even if two objects contain the same attributes and the same values, they are not equal. Arrays that are completely equal to each index element are not equal.
var o = {x:1}, p = {x:1//falsevar a = [],b=//False
Object comparisons are reference comparisons, and they are equal only when they reference the same object:
var a = []; var b = a;b[0] = 1; a[//1//true
To get a copy of an object or array, you must explicitly copy each property or element:
var a = ["A", "B", "C"]; var b = []; for (var i=0;i<a.length;i++) { = A[i]}
Similarly, if you want to compare two individual objects or arrays, you must explicitly compare each of their attributes or elements:
function Equalarrays (A, b ) {ifreturnfalse// unequal array unequal for (var i=0;i < a.length;i++) { ifreturnfalse // any elements that are unequal are not equal } returntrue;
Data Type Overview