First, JS data type
String, number, Boolean, Array, Object, Null, Undefined
1. JS has a dynamic type
The same variable can be used as a different type
var x // x is undefinedvar x = 6; // x is a number var x = "Bill"; // x is a string
2. Data type
- string
to store characters, either single or double quotation marks
- number
with or without (support scientific notation)
- Boolean
True or nbsp False
- Array
// first create re-assignment var cars=new Array (); cars[ 0]= "Audi" ;cars[ 1]= "BMW" ;cars[ 2]= "Volvo" // assign a value at the same time as created var cars=new Array ("Audi", "BMW", "Volvo " // direct assignment
var cars=["Audi", "BMW", "Volvo";
- Object
Delimited by curly braces, the properties of an object are defined in the form of name and key-value pairs, and attributes are separated by commas
var person={ "Bill", LastName "Gates", ID : 5566 };
// two ways of addressing name=person.lastname;name=person["LastName"];
- Undefined
When the declared variable has not been initialized, the default value for the variable is undefined // typical usage i;i // function f (x) { Console.log (x)}f () // when calling a function, you should provide The parameter is not provided, such as undefined var o = new Object (); O.P // var x = f (); x //
Undefined and null
Null is a placeholder for an object that does not exist
ECMAScript that undefined are derived from null, so they are defined as equal.
Distinguish:
Concole.log (null = = = undefined); // concole.log (typeof null typeof//False
Second, JS data type conversion 1. Conversion functions
- Convert to String (ToString)
// NumbervarINum = 10; alert (inum.tostring ()); //output "Ten"//BooleanvarBOOL =false; alert (bool. toString ()); //output "false"//Base ModevarINum = 10; alert (inum.tostring (2));//output "1010"Alert (inum.tostring (8));//Output ""Alert (inum.tostring (16));//output "A"
- Convert to Digital
parseint () parsefloat ()/*The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns NaN, and no further action is taken. *//*However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the parseint () converts the string before the character to a number. */varINUM1 = parseint ("12345red");//back to 12345varINUM1 = parseint ("0xA");//return tenvarINUM1 = parseint ("56.9");//returns 56 decimal point is an invalid charactervarINUM1 = parseint ("Red");//return NaN//the Parsefloat () method is similar to the parseint () method//But the first occurrence of the decimal point is a valid charactervarFNUM4 = parsefloat ("11.22.33");//back to 11.22
2. Forcing type conversions
3 types of coercion type conversions available in ECMAScript: Boolean, Number, String
- Boolean (value)
// When the value to be converted is a string of at least one character, not 0 digits, or an object, the Boolean () function returns true // If the value is an empty string, number 0, undefined, or null, it returns false var B1 = Boolean (""); // false-empty string var b2 = Boolean ("Hello"); // true-Non-empty string
- Number (value)
// the coercion type conversion of the number () function is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value. var iNum1 = parseint ("56.9"); // back to var fNum2 = parsefloat ("11.22.33"); // back to 11.22 var iNum3 = number ("56.9"); // back to 56.9 var fNum2 = number ("11.22.33"); // return NaN
- String (value)
Can convert any value to a string
Third, JS data type judgment 1. TypeOf
type |
structure |
/thead>
Undefined |
"Undefined" |
Null |
"Object"   (see below) |
boolean |
"boolean" |
numeric |
"number" |
string |
"string" |
Symbol (ECMAScript 6 new) |
"symbol" |
Host object (provided by the JS environment, such as a browser) |
implementation-dependent |
Function Object (implements [[Call]] in ECMA-262 terms) |
"function" |
Any other object |
"Object" |
2. instanceof (judging known object type) instance: instance, example, so instanceof used to judge
whether a variable is an instance of an object, is a three-mesh op-instanceof operator that identifies the type of object being processed, requiring the developer to explicitly confirm that the object is in use for a particular type
instanceof detects variable type, we are not detected by number, ' string ', type of bool
varA =[], b=NewDate ();functionC (name) { This. Name =name;} Console.log (AinstanceofArray)---------->trueAlert (binstanceofDate)---------------->trueAlert (cinstanceofFunction)------------->trueAlert (cinstanceof function)------------->false//Note: The instanceof must be followed by the object type, and the casing cannot be wrong, and the method is suitable for some conditional selection or branching.
3. Constructor (judging by the constructor of the object)
Definition: The constructor property returns a reference to the array function that created the object (the constructor that corresponds to the object returned)
constructor is originally a property on a prototype object, pointing to the constructor. However, depending on the order of the instance objects, if there is no instance property or method on the instance object, it is searched on the prototype chain, so the instance object can also use the constructor property.
varA =NewArray (); Console.log (ainstanceofArray)//A is an instance of array trueConsole.log (A.constructor = = Array)//whether the constructor for a instance corresponds to array true//ExamplefunctionDog (name) { This. name=name;}varDollar=NewDog ("Dollar"); Console.log (Dollar.constructor); //output function Dog (name) {this.name=name;}functionDog () {}varTim =NewDog (); Console.log (Tom.constructor= = = Dog);//true//The constructor property can be modified, causing incorrect results to be detectedfunctionDog () {}functionCat () {}cat.prototype=NewDog ();varm=NewCat (); Console.log (M.constructor==CAT);//falseConsole.log (John.constructor==person);//true//instanceof True for a direct or indirect referenceConsole.log (MinstanceofCat);//trueConsole.log (Johninstanceofperson);//true
4. Object.prototype.toString.call
functionvar toString = object.prototype.tostring;console.log (tostring.call ( New Date) = = = ' [object Date] '); // trueconsole.log (Tostring.call (new String) = = = ' [object String] '); // trueConsole.log (Tostring.call (a) = = = ' [object Function] '); // true
5. $.type () (Universal judgment)
Jquery.type (undefined) = = = "undefined" // truejquery.type () = = = = "undefined" // true null ) = = = "Null" // truetrue ) = = = "Boolean" // true
If you have suggestions or supplements, please contact us
Reference:
Http://www.jb51.net/article/73566.htm
JS data type