基本类型
- Boolen
- Number
- String
- Null
let qswj = null
- Undefined
let ppia;
console.log(ppia); // => undefined 未赋值
console.log(failed); //报错, 未声明
let drame = {};
console.log(drame.name) // => undefined
- Symbol
对象Object
除了以上6个外 都是 Object
eg:
Object.prototype.toString.call([]) ==>[object Array]
还有其他一些对象: 函数 Function、数组 Array、日期 Date、正则 RegExp、全局 Global、错误 Error
null && undefined
一. 定义 null 是 javascript 的关键字,表示一个特殊值,常用来描述"空值",typeof 运算返回"object",所以可以将 null 认为是一个特殊的对象值,含义是"非对象"。
undefined 是预定义的全局变量,他的值就是"未定义", typeof 运算返回 "undefined"
typeof null; // "object"
typeof undefined; // "undefined"
二. 转义 转换成 Boolean 时均为 false,转换成 Number 时有所不同
!!(null); // false
!!(undefined); // false
Number(null); // 0
Number(undefined); // NaN
null == undefined; //true
null === undefined; //false
三 判定
isNull = function (obj) {
return obj === null;
}
isUndefined = function (obj) {
return obj === void 0;
}
四. 用法 null 常用来定义一个空值
undefined 典型用法是:
变量被声明了,但没有赋值时,就等于 undefined。
var test;
console.log(test); //undefined
调用函数时,应该提供的参数没有提供,该参数等于 undefined。
//类如jQuery最外层IIFE用法
//这里是为确保undefined的值,因为es3中undefined可以赋值,es5才做了修正,变为只读
(function( window, undefined) {
})(window)
对象没有赋值的属性,该属性的值为 undefined。 var test = {} console.log(test.a); // undefined 函数没有返回值时,默认返回 undefined。
function test() {}
test(); //undefined
解构赋值时,undefined会使用默认值
const s = { a: null }
const { a = 'a', b = 'b' } = s;
console.log(a, b); // null 'b'