Js 運算子(加減乘除)
1.加法
var box=1+2 //3
var box=1+NaN //NaN,有一個為NaN就為NaN
var box=Infinity+Infinity //Infinity
var box=-Infinity + -Infinity //-Infinity
var box=Infinity + -Infinity //NaN,正無窮和負無窮相加為NaN
var box=100+ ‘100’; //100100,有一個為字串相加為字串拼接
var box=’你的年齡是’+10+20; //你的年齡是1020,後面的整形已轉換成字串
var box=’你的年齡是’+(10+20); //你的年齡是30,後面的整形先相加在轉換吃字串
var box=10+20+’是你的年齡’; //30是你的年齡,先相加,在轉換成字串
var box=10+物件; //10[object Object],如果有toString()或valueOf()則返回10+返回的值
注:++和–一樣
var box=1;
box++; //box=2,box=box+1
++box; //box=3,box=box+1
var age=box++ //box=4;age=3,先賦值在運算
var pox=++box //pox=5;box=5,先運算在賦值
2.減法
var box=100-70; //30
var box=-100-70; //-170
var box=-100–70; //-30
var box=-100-NaN //NaN
var box=Infinity-Infinity //NaN
var box=-Infinity–Infinity //NaN
var box=Infinity–Infinity //Infinity
var box=-Infinity-Infinity //-Infinity
var box=100-true; //99,true轉換成數值為1
var box=100-”; //100,”轉換為0
3.除法
var box=100/70 ///1.42….
var box=100/NaN ///NaN
var box=Infinity/Infinity ///NaN
var box=-Infinity/Infinity ///NaN
var box=-Infinity/-Infinity ///NaN
var box=100/true ///100,true轉換成1
var box=100/” /// Infinity
var box=100/null ///Infinity
var box=100/’lee’ ///NaN
var box=100/物件 ///NaN ,如果有toString()或valueOf(),返回10/返回值
4.求模
var box=10%3; //1,餘數為1
var box=100%NaN; //NaN
var box=Infinity%Infinity //NaN
var box=100%true //0
var box=100%” //NaN
var box=100%null //NaN