JS——变量类型和计算

来源:互联网 发布:linux下怎样输入中文 编辑:程序博客网 时间:2024/06/05 07:33

变量类型

JS按照存储方式,分为值类型和引用类型。


值类型 && 引用类型


值类型(Number、Boolean、String)

var a = 100;var b = a;a = 200;console.log(b); // 100



引用类型(Object、Array、Function)

var a = {age:20};var b = a;b.age = 21;console.log(a.age); // 21



typeof运算符


JS中使用typeof能得到的类型


typeof只能得到值类型的详细类型以及引用类型的function

typeof undefined   // undefinedtypeof "abc"       // stringtypeof 123         // numbertypeof {}          // objecttypeof []          // objecttypeof null        // objecttypeof function    // function



变量计算(值类型)——强制类型转换


字符串拼接


var a = 100 + 10;   //110var b = 100 + "10"; // "10010"


==运算符


何时使用 ===和==

当需要判断是否为空的时候,使用==null,这样可以判断是否为null、undefined

其余时候使用===

100 == "100" ; // true0 == " " ;     // truenull == undefined  // true