1、typeof 操作符来查看 JavaScript 变量的数据类型
typeof 变量名 //返回变量类型, 如果变量名没有声明返回 undefined
例如:
typeof [1,2,3,4] // 返回 object
typeof {name:'姓名', age:34} // 返回 object
typeof new Date() // 返回 object
typeof function () {} // 返回 function
typeof null // 返回 object
2、js判断一个变量是不是数组
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
3、js判断一个变量是不是日期
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
4、undefined 和null的区别
undefined 是所有没有赋值变量的默认值,自动赋值。
null是一个只有一个值的特殊类型。表示一个空对象引用。
当使用完一个比较大的对象时,需要对其进行释放内存时,设置为 null。
5、Symbol
ES6 引入了一种新的原始数据类型 Symbol ,表示独一无二的值,最大的用法是用来定义对象的唯一属性名。
let a = Symbol("123");
let b='123'
console.log(a) //返回Symbol(123)
console.log(a==b) //返回false