JS基本数据类型以及基本运算

//定义变量
var num = 2;
var num2 = 10.5;
var sex = '男';
    sex2 = '女';
var result = true;
var result2 = false;
var str = 'Asshole';
var nullValue = null;
var undefineValue = undefined;

//输出变量的值
console.log(num,num2,sex,sex2,result,result2,str,nullValue,undefineValue);
//2 10.5 "男" "女" true false "Asshole" null undefined

//输出变量的类型
console.log(typeof num,typeof num2,typeof sex,typeof sex2,typeof result,typeof result2,typeof str,typeof nullValue,typeof undefineValue);
//number number string string boolean boolean string object undefined

//数据类型的基本运算
var newSex = sex + sex2;//男女
console.log("newSex:" + newSex);

var newNum = num + num2;//12.5
console.log(newNum);

var newResult = result || result2;//true
console.log(newResult);

var newResult2 = result && result2;//false
console.log(newResult2);

var newNum2 = num + num2 + sex;//12.5男
console.log(newNum2,typeof newNum2);//最终newNum2变成了字符串型

var newNum3 = num + result;//3
console.log(newNum3);//boolean类型会变成number类型相加

var sub1 = 10 + 10 + '10';//2010
var sub2 = '10' + 10 + 10;//101010
var sub3 = (10 + '10') + 10;//101010
console.log(sub1,sub2,sub3);//任何数据类型和字符串拼接,都会强转成字符串类型

results matching ""

    No results matching ""