js中变量类型和变量计算

来源:互联网 发布:一句话木马 php 编辑:程序博客网 时间:2024/06/05 09:19

一、变量类型

1.值类型

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

2.引用类型:对象、数组、函数

var a = {age:20};var b = a;b.age =21;console.log(a.age);//21
3.typeof运算符

typeof undefined // undefinedtypeof 'abc' // stringtypeof 123 // numbertypeof true //booleantypeof {} // objecttypeof [] // objecttypeof null // objecttypeof console.log // function

typeof 能够区分值类型的类别:string number boolean , 不能区分对象(其中null也是对象),function除外

二、变量计算

强制类型转换

1.字符串拼接

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

2.==运算符

100 == ‘100’ // true0 == '' // truenull == undefined // true

3.if语句

var a = true;if(a){        // ...}var b = 100;if(b){        // ...}var c = '';if(a){        // ...}

4.逻辑运算

console.log(10 && 0); // 0console.log('' || 'abc'); // 'abc'console.log(!window.abc); // truevar a = 100console.log(!a); // falseconsole.log(!b); // true




原创粉丝点击