JavaScript数据类型
JavaScript数据类型:基本概念
常用的数据类型有7种,分别是 number、string、boolean、undefined、object、array、function。
ES6中新增了一种symbol,这里不说。
JavaScript数据类型:object包含类哪些类型
object包含了data、function、array等。
JavaScript的基本数据类型和引用数据类型分别有哪些
基本数据类型有:string、number、undefined、boolean、
基本数据类型的实例:
var a="hello";
//判断数据类型的常用方法typeof();
alert(typeof(a));
var b=123;
alert(typeof b);
var c=true;
alert(typeof c);
var d=null;
alert(typeof d);
var f=undefined;
alert(typeof f);
var g=123;
alert(b==g); //ture
alert(typeof (b==g)); //boolean
var e=[1,2,'hello','你好',true,null,undefined];
//数组是一个复合数据类型;里面可以存放所有的基础数据类型
alert(typeof e);
var h={name:"xx",age:21,sex:"boy",tedian:"handsome",say:true};
//json是一个复合数据类型;里面可以存放所有的基础数据类型(键值对)
alert(typeof h);
var i=[1,2,'hello','你好',true,null,undefined];
alert(e==i);
//隐式数据类型转换
var a='8',b=2,c='hello',f=false,g[];
alert(typeof(a=b)); //string
//+''
alert(b+''); //2
alert(typeof(b='')); //string
//-0
alert(a-0) //8
alert(typeof (a-0)); //number
//*1
alert(a*1); //8
alert(typeof (a*1)); //number
alert(f-b); //-1
alert(typeof(f+a)); //(f+a)为false8---类型为string
alert(typeof(f+b)); //f+b为2----类型为number
alert(g+b);
var d=c-0;
alert(d); //NaN是一个不是数字的数据类型
alert(typeof d); //number
alert(d==d); //NaN自己不认识自己
alert(isNaN(d)); //true
alert(isNaN(b)); //false
引用数据类型有:object、function、array等。
JavaScript中typeof输出分别是什么
{}、[]输出object。
console.log()输出function。
1、假设: Number(‘as’) == NaN ?
肯定是false, Number(‘as’) 输出 NaN。
注)用 isNaN() 检测是否是非数值型。
请注意这里 isNaN 帮我转成 true 。
期间我一直在纠结 Number(‘as’) 输出 NaN ?NaN == NaN 为什么是 false。其实 js 规定的NaN 不等于NaN。
2、假设:Number(‘123’) == NaN ?
JS数据类型:如何判断数据类型?
1、typeof 操作符(通用:上面有内容有讲到)
2、toString ( )
作用:其他类型转成 string 的方法
支持:number、boolean、string、object
不支持:null 、undefined
3、toLocaleString ( )
把数组转成本地字符串
4、检测数组类型的方法
① instanceof 操作符
②对象的 constructor 属性
③ Array.isArray( ) 检验值是否为数组
就先到这里了。