[day-3]布林、undefined、null/字串數字轉換/比較與邏輯運算子


Posted by Xiang on 2021-10-22

布林值

只有兩種 判斷什麼是對 什麼是錯
判斷式會產出布林值 true or false

true
false
console.log(3>2) //則會產出 true 這個布林值

undefined

尚未被賦予值

let a ;
console.log(a); //則會輸出undefind

若用tpyeof去查詢
console.log(typeof a); //也會輸出underfind


null

有被賦予值,是告知為空值
比較常用在原本有賦予值之後要把他清空則可以用null
使用null清空資料,可以釋放記憶體空間

let b = 1;
let c = 1234;
c = null ;
console.log(c) //則會輸出null

字串轉數字方法

parseInt( ); 字串轉資料

let a = “1”;
a = parseInt(a) //此時a從字串1轉換為數字1
console.log(a+1); //則可以算出2

#若用非數字如英文字串用parseInt則會輸出NaN


數字轉字串方法

.toString( ); 數字轉字串

let b = 1;
b = b.toString( ); //此時將b轉為字串
console.log(typeof b); //查詢結果輸出為String
console.log(b+1); //則會輸出11

比較運算子

依照流程判斷的情境去做使用 回傳的結果為布林值true or false

>大於
<小於
> = 大於等於
< = 小於等於
== 等於
! =不等於

=、== 差異

一個等於 = 是賦予值的意思

let a = 1; //( 宣告a賦予1的值 )

兩個等於 == 才是比較運算子的等於

let a = 1;
let b = 2;
console.log(a==b) //輸出false
console.log(a!=b) //輸出true

==、=== 差異

兩個等於==會協助轉型

let a = 1;
let b = “1”;
console.log( a==b) //輸出true因為他會自動轉型成相同型別

邏輯運算子

邏輯運算子 && ||
&&同時滿足條件

let a = 1;
let b =2;
console.log(a==2&&b==2); //回傳false 因為沒有兩個條件都達成

||滿足其中一個條件

let c = 3;
let d =4;
console.log(c==4 || d==4); //回傳true 因為其中d==4有符合條件

可同時使用多個邏輯運算子

console.log(a==1 && b==2 && c ==4); //回傳flase 因為沒有完全滿足條件

#javascript #js #web front end







Related Posts

人性較量Day06~量化資訊

人性較量Day06~量化資訊

ES6 語法中有什麼新東西?

ES6 語法中有什麼新東西?

Python 爬取 Google 新聞

Python 爬取 Google 新聞


Comments