1 /**2 * Type Conversion3 *4 * JavaScript Data type5 * 1. Different data types6 * String7 * Number8 * Object9 * BooleanTen * Function One * A * 2. Object Type - * Object - * Date the * Array - * - * 3. Data types that do not contain any values - * NULL + * Undefined - * + * typeof operator View JS data type A * at * Constructor (constructor) Properties - * */ - -Console.log (typeof(' John '));//string -Console.log (typeof(3.14));// Number -Console.log (typeof(NaN));// Number inConsole.log (typeof(false));//Boolean -Console.log (typeof([1,2,3,4]));//Object , an array is also an object toConsole.log (typeof({name: ' John ', age:34});//Object +Console.log (typeof(NewDate ()));//Object -Console.log (typeof(function() {}));//function theConsole.log (typeof(MyCar));//undefined *Console.log (typeof(NULL));//Object,null is an empty object $ Panax Notoginseng /** - Please note: the the data type of NaN is number + the data type of the array is Object A Date data type is Object the the data type of NULL is Object + the data type of the undefined variable is undefined - */ $ $ //if the object is an array or date, it is not possible to use TypeOf to determine their type, because all returns object - - /**constructor (constructor) Properties*/ the //The constructor property returns the constructor for all JavaScript variables -Console.log (' John ' constructor);//function String () {[native code]}WuyiConsole.log ((3.14). constructor);//function Number () {[native code]} theConsole.log (false. constructor);//function Boolean () {[native code]} -Console.log ([1,2,3,4].constructor);//function Array () {[native code]} WuConsole.log ({name: ' John ', age:34}.constructor);//function Object () {[native code]} -Console.log (NewDate (). constructor);//function Date () {[native code]} AboutConsole.log (function() {}.constructor);//function function () {[native code]} $ - //use the constructor property to see if an object is an array or a date - functionIsArray (arr) { - returnArr.constructor.toString (). IndexOf (' Array ') >-1; A } + the - functionisDate (date) { $ returnDate.constructor.toString (). IndexOf (' Date ') >-1; the } the theConsole.log (IsArray ([+]));//true theConsole.log (IsArray (' 456 '));//false - inConsole.log (IsDate (NewDate ()));//true theConsole.log (isDate (' VVV '));//false the About //convert numbers to Strings the //String (MyNumber) Global method the //mynumber.tostring () Number Object method the //more Number Object methods: Http://www.runoob.com/jsref/jsref-obj-number.html +Console.log (typeof(String (123)));//string -Console.log (typeof((+ +). ToString ()));//string the Bayi //Convert a Boolean value to a string theString (false); theString (true); - - false. toString (); the true. toString (); the the //Convert a date to a string the //In the date method, you can see more functions for converting dates to strings - //Date method: Http://www.runoob.com/jsref/jsref-obj-date.html theConsole.log (String (Date ()));//Tue Mar 23:11:01 gmt+0800 (China Standard Time) theConsole.log (Date (). toString ());//Tue Mar 23:11:25 gmt+0800 (China Standard Time) the 94 //Convert a string to a number the //Global Method Number () the //In the number method, you can see more ways to convert strings to numbers the //Number method: Http://www.runoob.com/jsref/jsref-obj-number.html98Console.log (Number (' 3.14 '));//3.14 AboutConsole.log (Number (""));//0 -Console.log (Number (""));//0101Console.log (Number (' 99 88 '));//NaN102 103 //Unary operator +104 vary = ' 5 '; theConsole.log (typeof(+y));// Number106 107 //if it cannot be converted, it will still be a number, but the value is Nan108 varz = ' John ';109Console.log (typeof(+z));// Number theConsole.log (+Z);//NaN111 the //Convert a Boolean value to a number113Console.log (Number (false));//0 theConsole.log (Number (true));//1 the the //Convert a date to a number117Console.log (Number (NewDate ()));//1459265021379118Console.log (Number (NewDate (). GetTime ()));//1459265068371119 - //Automatic conversion type Conversion121 //When JavaScript attempts to manipulate an "error" data type, it is automatically converted to the "correct" data type. 122Console.log (5 +NULL);//5, NULL is 0123Console.log (' 5 ' +NULL);//5null124Console.log (' 5 ' + 1);//Wuyi theConsole.log (' 5 '-1);//4126 127 //when you try to output an object or a variable, JavaScript automatically calls the ToString () method of the variable: -document.getElementById ("Demo"). InnerHTML =MyVar;129 the //if MyVar = {name: "Fjohn"}//toString is converted to "[Object Object]"131 //if MyVar = [1,2,3,4]//toString is converted to "1,2,3,4" the //If MyVar = new Date ()//toString converted to "Fri Jul 09:08:55 gmt+0200"133 //numbers and Booleans are also often converted to each other:134 //if MyVar = 123//toString is converted to "123"135 //If MyVar = TRUE//toString is converted to "true"136 //if MyVar = False//toString is converted to "false"
(v) JavaScript [type conversion]