js数据类型

  • 1分类
    基本数据(值)类型:
    String :任意字符串
    Number :任意数字
    Boolean :true/false
    Undefined "undefined
    Null :null
    对象(引用)类型
    Object :任意对象
    Function :一种特别的对象(可以执行)
    Array:一种特别的对象(数字下标 ,内部数据是有序的)
    Date类型
    RegExp类型
    基本包装类型(Boolean String Number(通过new 构造函数创建))
    单体内置对象(Global Math)

  • 2判断
    typeof 返回数据类型的字符串表达(小写)
    可返回的数据类型: number string boolean undefined function(函数) object(对象,数组和null等)
    所以可以用typeof判断 number string boolea undefined 函数
    instanceof 判断对象的具体类型 (a instanceof b 判断a是不是b的实例)
    === 可判断null和undefined
    constructor属性可以返回所有js变量的构造器函数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>js数据类型</title>
	</head>
	<body>
		<script type="text/javascript">
			/*1.基本数据类型判断*/
			var a
			console.log(typeof a)  //undefined
			a=2
			console.log(typeof a) //number
			a=NaN
			console.log(typeof a)  //number
			a="123"
			console.log(typeof a) //string
			a=true
			console.log(typeof a)  //boolean
		    a=null
			console.log(typeof a)  //object
			
			//判断null可以使用===
			console.log(a===null)  //true
			
			/*2引用数据类型判断*/
			var b1 = {
				b2:[1,'abc',console.log],
				b3:function(){
					console.log('hi')
					return function(){
						return 'lalala'
					}
				}
			}
			console.log(b1 instanceof Object,b1 instanceof Array) //true false
			console.log(b1.b2 instanceof Object,b1.b2 instanceof Array) //true true
			console.log(b1.b3 instanceof Object,b1.b3 instanceof Function) //true true
			
			//判断函数还可以用typeof
			console.log(typeof b1.b3 =="function") //true
			
			
			/*****constructer属性*******/
			console.log(b1.b2.constructor ==  Array)//true
			
			
			/*-------对b2数组中元素console.log的理解----------*/
			console.log(typeof b1.b2[2])  //function
			//已知是一个函数.加括号就可以执行
			b1.b2[2](1) //1
			b1.b3() //hi  
			//在b1.b2()之后得到了function(){	return 'lalala}
			console.log( b1.b3()() )  //lalala
			
			
		</script>
		
		
	</body>
</html>


  • 1. undefined 与null 的区别?
    undefined :定义了未赋值
    null :定义并赋值了,值为null.
    2. 什么时候变量赋值为null?
    初始赋值为null,表明将要赋值成对象
    最后赋值为null,释放变量占用的内存(让变量指向的对象成为垃圾对象,被浏览器垃圾回收)
    3. 严格区别变量类型与数据类型?
    数据的类型:
    基本类型
    对象类型
    变量类型:
    基本类型 :保存的就是基本类型的数据
    引用类型 :保存的就是地址值
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值