1、js中的数据类型
Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object。code
Number数据类型指的是数字,包括整型和浮点数。如对象
var a=1,b=2.0;
String表示单引号或者双引号包括起来的0及0个以上的字符,能够是字母 、数字、标点、特殊字符、空格、组成。如:token
var name = 'yang' console.log(name) // 'yang'
Boolean布尔类型只有true和false,用于条件判断。如:字符串
var flag = true; if(flag) { console.log('flag is true') }else { console.log('flag is false') }
null与undefined均可以表示“没有”,含义很是类似string
if (!undefined) { console.log('undefined is false'); } // undefined is false if (!null) { console.log('null is false'); } // null is false undefined == null // true
对象就是一组“键值对”(key-value)的集合,是一种无序的复合数据集合。console
var obj = { foo: 'Hello', bar: 'World' };
String(x)class
String(1)//"1" String(true)//"true" String(null)//"null" String(undefined)//"undefined" String({})//"[Object Object]"
toString(x)object
(1).toString() //"1" true.toString() //"true" null.toString() //Uncaught TypeError: Cannot read property 'toString' of null // at <anonymous>:1:6 undefined.toString() //Uncaught TypeError: Cannot read property 'toString' of undefined //at <anonymous>:1:11 {}.toString() //Uncaught SyntaxError: Unexpected token . ({}).toString() //"[object Object]"
特殊用法数据类型
1+'' // "1" true+'' // 'true' null+'' // 'null' undefined+'' // 'undefined' {}+'' // 0 var o = {} o+ '' //{object Object}
Number(some) parseInt(some) parseFloat(somw) x-0 +x
Boolean(some) !!some
0 NaN '' null undefined