js的值类型和引用类型

(1)值类型:String、Boolean、Number、null、undefined。(原始值)

var a = 2;

var b = a;

b=3;

 a ==>2; b  ==>3

原始值是不可改变的,每一个变量都存不同的地址

(2)引用类型:对象(Object)、数组(Array)、函数(Function)。(对象引用是可变的)

应用类型当且仅当引用同一基对象才相等,(同一个引用地址)

typeof用于检测简单类型(typeof会返回一个变量的基本类型)

alert(typeof '111'); // "string" 

alert(typeof 22); // "number" 

alert(typeof a); // "undefined" 

alert(typeof undefined); // "undefined" 

alert(typeof null);//"object"

alert(typeof []); // "object"

alert(typeof function(){}) ;//"function"

typeof不适合用于判断是Array还是Object,这两个都会判断是object

如果我们想要判断一个变量是否存在

if(a)    erroer :Uncaught ReferenceError: m is not defined(若a未声明,则报错)

if(typeof a != 'undefined')  不会报错

 

instanceof 用于检测实际类型(instanceof返回的是一个布尔值---instanceof 用于判断一个变量是否某个对象的实例)

user instanceof Object

users instanceof Array

原文地址:https://www.cnblogs.com/sunnie-cc/p/6801331.html