1.數(shù)據(jù)類型的轉(zhuǎn)換
boolean,string-->number
強(qiáng)制轉(zhuǎn)換Numbe()
Number() -->只能識(shí)別純數(shù)字的字符串恩沽,只要帶了英文的字符串孕索,都輸出NaN
"20px" -->NaN
NaN -->不是一個(gè)數(shù)字
<script>
var a = true;
var b = false;
var c = "20";
var d ="20px";
var e = "good"
console.log(Number(a));
console.log(Number(b));
console.log(Number(c));
console.log(Number(d));
console.log(Number(e));
</script>
2.自動(dòng)轉(zhuǎn)換
<script>
/*
自動(dòng)轉(zhuǎn)換(隱式轉(zhuǎn)換)
在算術(shù)計(jì)算中的自動(dòng)轉(zhuǎn)換: 先將數(shù)據(jù)轉(zhuǎn)為number嘿辟,再進(jìn)行計(jì)算
+垂谢,-氨菇,*假哎,/,%
*/
var a = 20;
var b = true;
var c = a-b;
// var c = a - Number(b);
console.log(c);
/* 特殊+
只要一邊為字符串歉嗓,那么+起到拼接符的作用丰介,結(jié)果為字符串
*/
var d = "20";
var e = 1;
console.log(d+e);
</script>
<script>
/*
比較運(yùn)算中的自動(dòng)轉(zhuǎn)換:
先將數(shù)據(jù)轉(zhuǎn)為number,再比較,結(jié)果返回boolean值
<,>,==,!=,<=,>=
*/
var a = "20";
var b = true;
console.log(a>b);
// console.log(Number(a)>Number(b));
</script>
3.parseInt(),parseFloat()
<script>
/*
string--number
parseInt()
parseFloat();
被轉(zhuǎn)換的字符串第一位必須為數(shù)字
*/
var a = "12.343px";
var e = "a12.px"
var b = parseInt(a);
var c = parseFloat(a);
console.log(b);
console.log(c);
console.log(parseInt(e));
</script>
4.強(qiáng)制轉(zhuǎn)換
<script>
/*
1.強(qiáng)制轉(zhuǎn)換
number,string,boolean
*/
/*
其他-->number
Number()
string--number
parseInt()
parseFloat()
*/
/*
其他-->boolean
Boolean()
只有5種情況為false
0哮幢,"",NaN,null,undefined
*/
var a = "hello world";
var c = 1;
var b = Boolean(a);
console.log(b);
console.log(Boolean(""));
/*
其他-->string
String()
toString()
*/
var e = 20;
var f = String(e);
var g = e.toString();
console.log(typeof f);
console.log(typeof g);
</script>
5.算術(shù)带膀,比較,邏輯運(yùn)算
<script>
/*
發(fā)生在運(yùn)算中
算術(shù)運(yùn)算+橙垢,-垛叨,*,/柜某,%嗽元,++,--
比較運(yùn)算>,<喂击,== 結(jié)果返回boolean
邏輯運(yùn)算&&剂癌,||,翰绊!
&& 兩邊都為true佩谷,結(jié)果為true
|| 只有一邊為true,結(jié)果為true
监嗜! 取反
先將數(shù)據(jù)轉(zhuǎn)為number谐檀,再進(jìn)行比較
*/
var a = 10;
var b = 20;
var c = 30;
var d = 40;
console.log(a*b);
console.log(a/b);
console.log(a%b);
c++;
console.log(c);
d--;
console.log(d);
console.log(c%d);
console.log(a<b && b<c);
console.log(a<b || b>c);
console.log(!(a>b));
</script>
賦值,三目運(yùn)算
<script>
/*
賦值運(yùn)算
=裁奇,+=稚补,-=,*=框喳,/=课幕,%=
三目運(yùn)算
var f = (d<c)>
*/
var a = 10;
var b = 20;
b +=a;
// b=b+a;
console.log(b); // 30
b -=a;
// b=b-a
console.log(b); //20
var c = 20;
var d = 30;
var f = (d>c)?"d大于c":"d小于c";
console.log(f);
</script>