In JavaScript, data types fall into two categories: the base data type and the reference data type. In JavaScript, declare variables using the keyword var.
I. Basic data types
Javascrip contains five basic data types:undefined,null,boolean, Number , and string.
- Boolean: Boolean, value is
true
orfalse
- Number: Numeric value for any integral type floating point value
- string: A single character or consecutive character that is enclosed by single or double quotation marks (JavaScript is not distinguished by character type)
- null: null type, with only one value:null
- undefined: Not defined, it has only one value:undefined
typeof keyword : Because the variables in JavaScript are loosely typed, it provides a way to detect the data type of the current variable, that is, the typeof keyword, Of the 5 simple data types mentioned in ECMAScript (these 5 are just data types , representing a data type, like the int,string type in C #), by typeof keyword, The following values (shown as strings) are returned for these 5 types of data
- "boolean"--if the value of the variable is a boolean type;
- "Number"-The value is a numeric type;
- "string"-the value is a string type;
- "Object"-- object or value is null;
- "undefined"-- not stated , or the value of the variable is undefined or uninitialized;
The value of the primitive type is stored directly in the variable and can be detected with typeof . However, the typeof null detection is to return an object instead of returning null . Therefore, when NULL is detected, it is best to use all equals (= = =), which also avoids coercion of type conversions.
string number
and   boolean
The original package type is automatically created. For example:
1 var name = "Tom"; 2 var char = Name.charat (0); 3 console.log (char); // "T"
Things that happen in the JavaScript engine:
1 var person = "Tom"; 2 var New String (person); 3 var char = Temp.charat (0); 4 NULL ; 5 console.log (char); // "T"
A reference to a string object is destroyed immediately after it is exhausted, so a property cannot be added to the string, and falseis returned when instanceof detects the corresponding type:
1 var person = "Tom"; 2 person.age = +; 3 Console.log (person.age); // undefined 4 instanceof String); // false
II. Reference data types
reference type : Saved as an object, essentially a reference to a memory location, so the object is not saved in a variable. In addition to custom objects, JavaScript provides a variety of built-in types.
Object Type: Theobject type is the originator of the JavaScript reference type, which is the same as in C # and Java, where you can add properties and methods after creating an instance of type object.
Based on Object , JavaScript provides built-in types:
- Array: An ordered list of arrays of type, indexed by number, of a set of values
- Date: Day and Time type
- error: Run-time error type
- function: Types of functions
- RegExp: Regular expression type
You can use new to instantiate each object, or to create an object in literal form:
1 varobj =NewObject ();2 varperson = {3Name: "Tom",4Sex: "Man",5"My Age": 226 };7Console.log (Person.sex);//Accessing Properties8Console.log (person["My Age"]); 9obj =NULL;//de-referencing
Obj does not contain an object instance, but rather a pointer (or reference) to the location of the actual object in memory. Because typeof returns object for all reference types of non-functions , it is necessary to use instanceof to detect reference types.
Object objects
An object is a reference type that is commonly used to create objects in two ways: the object constructor and the object literal form:
1 var p1 = {2 name: "Tom",3 age:224}; 5 var New Object (); 6 p2.name = "Jack";
In JavaScript, you can add properties to an object at any time:
1 p1.age = 0; 2 function () {3 alert (this. Name); 4 }5//"Tom"
There are two ways to detect if a property exists: in and hasOwnProperty (), which detects both the prototype attribute and its own (instance) attribute, which detects only its own (instance) attributes:
1 in p1); // true 2 Console.log (P1.hasownproperty ("Age")); // true 3 in p1); // true 4 Console.log (P1.hasownproperty ("toString")); // false
Object P1 does not define toString, which inherits from object, so in and hasownproperty () differences occur when the property is detected.
To delete a property, use the delete operator to delete the own property, and you cannot delete the prototype property:
1P1.tostring =function(){2Console.log ("P1 object");3 };4Console.log (P1.hasownproperty ("toString"));//true5P1.tostring ();//"Per1 Object"6 Deletep1.tostring;7Console.log (P1.hasownproperty ("toString"));//false8Console.log (P1.tostring ());//[Object Object]
JavaScript Data types