js中數據類型分為兩種,第一種基本的數據類型:Number,String,Boolean,Null,Undefiend;
第二種是復雜的數據類型,統稱object(引用類型)。
簡單說一下基本數據類型,不詳細介紹了
js中的number
JavaScript不區分整數和浮點數,統一用Number表示,例如:
1 ?//整數1
3.1415926 ?//小數
-12312 //負數
1.23798e12 ?//科學計數法
特殊的number類型
NaN //NaN表示Not a Number,當無法計算結果時用NaN表示
Infinity ?// Infinity表示無限大,當數值超過了JavaScript的Number所能表示的最大值時,就表示為Infinity
js中的string
"sdadasd" //字符串sdadasfd,用單引號或者雙引號包裹起來
js中的boolean
true //true,正確
false ?//false , 錯誤
js中的null和undifined
null ?//空值
undifined ?//未定義
下面介紹一下強制類型轉換
Number(obj) ?// 把obj轉化為number,如果沒辦法轉為number,則返回NaN,parseInt()和parseFloat()也是這樣,下文有詳細介紹
toString(obj) ?// 把obj轉化為字符串
Boolean(obj) ?//把obj轉化為布爾值
轉換規則
Boolean(undefined):false
Number(undefined):NaN
String(undefined):'undefined'
Boolean(null):false
Number(null):0
String(null):'null'
Number(true): 1 || Number(false) : 0
String(true):'true' || String(false):'false'
Boolean(undefined):false
Boolean(null):false
Boolean(非空對象包括空數組[]和空對象{}):true
Boolean(非0): true || Boolean(0和NaN):false
Boolean(非空包括空格字符串):true || Boolean(''):false
Number(true):1 || Number(false):0
Number(各種進制的數字):運算后的十進制的數字,如1.0或1.或01會以1輸出
Number(undefined):NaN
Number(null):0
Number(字符串):
Number(只包含數字的十進制和十六進制的字符串):運算后的十進制的數字
[注意]字符串中不識別八進制,按照十進制數字處理
Number(''和' '):0
Number(其他情況的字符串):NaN
Number(對象):
Number([]和[0]和[-0]):0
Number([數字]):運算后的數字
Number([1,2]和{}和其他對象):NaN
下面看一些表達式計算結果
3>2 ?//false
'2'!==2 ?//false
'2'!===2 ?//true
Infinity==Infinity //true
NaN==NaN ?//false
null==undefined ?//true
null===undefined ?//false
1==true ?//true
1===true //false
'1'==true ?//true
'1'===true ?//false
js中的運算
js中的運算符優先級
運算符 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 說明
.[ ] ( ) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 字段訪問、數組索引、函數調用和表達式分組
++ — – ~ ! ? ? ? ? ? ? ?delete new typeof void一元運算符、返回數據類型、對象創建、未定義的值
* / % ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 相乘、相除、求余數
+ – + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?相加、相減、字符串串聯
<< >> >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?移位
< <= > >= instanceof ? ? 小于、小于或等于、大于、大于或等于、是否為特定類的實例
== != === !== ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?相等、不相等、全等,不全等
& ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 按位“與”
^ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?按位“異或”
| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?按位“或”
&& ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 邏輯“與”
|| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?邏輯“或”
?: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?條件運算
= OP= ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?賦值、賦值運算(如 += 和 &=)
, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?多個計算
js中的隱式轉換
console.log("-------以下乘法---------");
console.log(5*"5");//25
console.log(5*"a");//nan
console.log(5*NaN);//nan
console.log(5*null);//0
console.log(5*undefined);//nan
console.log(5*5);//25
console.log("-------以上乘法---------");
// 1、如果2個數值都是數字,那么直接進行乘法運算,(相信大家都會的,和小學數學一樣,同時要注意數字的符號),如果乘積數值超過了ECMAscript的數值表示范圍,則返回Infinity(正無窮)或者-Infinity(負無窮)
//? 2、如果一個數是NaN,那么結果就是NaN
// 3、如果Infinity與0相乘,結果是NaN
// 4、假如一個操作符是數字,另外一個不是數值,那么先用Number()函數,將其進行轉化,將轉化出來的值與數字進行相乘。假如轉換出來的結果出現NaN,那么結果就是NaN。
//除法
console.log("-------以下除法---------");
console.log(5/"5");//1
console.log(5/"a");//nan
console.log(5/NaN);//nan
console.log(5/null);//in
console.log(null/5);//0
console.log(5/undefined);//nan
console.log(5/5);//1
console.log(5/0);//in
console.log(0/5);//0
console.log(0/0);//nan
console.log("-------以上除法---------");
// 和乘法類似,唯一多的一條就是0/0結果是NaN
//取余、求模
console.log("-------以下取余、求模--------");
console.log(16%"5");//1
console.log(5%"a");//nan
console.log(5%NaN);//nan
console.log(5%null);//nan
console.log(null%5);//0
console.log(5%undefined);//nan
console.log(5%5);//0
console.log(5%0);//nan
console.log(0%5);//0
console.log(0%0);//nan
console.log("-------以上取余、求模---------");
// 1、被除數是無窮大,除數是有限大的值,那么結果是NaN
// 2、被除數是有限大的值,除數是0,那么結果是NaN
// 3、Infinity%Infinity結果是NaN
// 4、被除數是有限大的值,除數是無窮大的值,結果是被除數。
// 5、被除數是0,結果是0
//加法
console.log("-------以下加法--------");
console.log(16+"5");//165
console.log(5+"a");//5a
console.log(5+NaN);//nan
console.log("5"+null);//
console.log(5+undefined);//nan
console.log(5+5);//10
console.log("兩個數的和是"+5+5);//55
console.log("兩個數的和是"+(5+5));//10
console.log("-------以上加法--------");
//? 1、有一個是字符串,那么另外一個也會轉換為字符串進行拼接。假如一個是字符串,另外一個是null或者undefined,那么相加,null或者undefined就會調用String()方法,獲得字符串“null”或者“undefined”,然后進行拼接。
// 2、假如一個數字加null或者undefined,那么還是把null或者undefined進行Number()轉換之后再相加。
// 3、剩下的原則和其他的差不多,就不多說了。
//減法
console.log("-------以下減法--------");
console.log(16-"5");//11
console.log(typeof 16-"5");
console.log(5-"a");//nan
console.log(5-NaN);//nan
console.log(5-null);//5
console.log(5-undefined);//nan
console.log(5-5);//0
console.log(5-true);//4
console.log(5-"true");//nan
console.log(5-"");//5
//alert(Number('sds'));nan
// alert(Number(''));//0
console.log("兩個數的差是"+5-5);//nan
console.log("兩個數的差是"+(5-5));//shi0
console.log("-------以上減法--------");
// 1、Infinity-Infinity結果是NaN
// 2、-Infinity-Infinity結果是-Infinity
// 3、一個數字減Infinity結果是-Infinity
// 4、Infinity-(-Infinity)結果是Infinity
// 5、如果操作數是對象,則調用對象valueOf方法,如果結果是NaN那么結果就是NaN。如果沒有valueOf方法,那么調用toString()方法,并將得到的字符串轉換為數值。
總結
js隱式轉化有很多坑,盡量用===替代==