1.数据类型的转换
number
var num1=10;
var num2=16;
string
var str1='king';
boolean
var boo=true;
var oo2=false;
undefined
var undef;
1)number + number = number
var num3=num1+num2;
console.log(num3);//26
console.log(typeof num3);
2)number + string = string
var str=num1+str1;
console.log(str);//10hello
console.log(typeof str);
3)number + boolean = number
var boo=num1+boo1;
console.log(boo);//11
console.log(typeof boo);
4)number + undefined = number
var und=num1+undef;
console.log(und);//NAN
console.log(typeof und);
5)string + boolean = string
var stb=str1+boo1;
console.log(stb); //kingtrue
console.log(typeof stb);
6)string + undefined = string
var sfind=str1+undef;
console.log(sfind);/kingundefined
console.log(typeof sfind);
2.数据类型转换减法操作
var num1 = 10;
var str1 = "11";
var str2 = "Hello";
var str3 = "35";
var str4 = "waht";
console.log(num1+str1);//1011
console.log(num1-str1);// -1
console.log(str1+str3);//1135
console.log(str3-str1);//24
console.log(str2-str4);//NaN
3.强制转换
整数
var num=6;
var str=num.toString();
console.log(str);//6
console.log(typeof str);//string
小数
var num=6.6;
var str1=parseInt(num);
console.log(str1);//6
console.log(typeof str1);//number
小数点后几位
var num=.0666;
var str2=parseFloat(num);
console.log(str2);//0.666
console.log(typeof str2);//number
字母
var str="abc";
var str3=isNaN(str);
console.log(str3);//true
console.log(typeof str3);//boolean
4.运算符-逻辑运算符
var num = parseInt(prompt("请输入一个数字"));
//判断 num 是否在 0~10之间
var result=(num>=0&&num<=10);
console.log(num+"在0~10之间吗?"+result);
网友评论