我有以下代碼:如何檢查兩個值是否與JavaScript不等於null?
var store = window.localStorage;
var a = store.getItem('AccountID');
var b = store.getItem('CityID')
我怎樣才能使它所以我refreshGrid( 「頁」);函數只能運行,如果這兩個都設置爲值?
我有以下代碼:如何檢查兩個值是否與JavaScript不等於null?
var store = window.localStorage;
var a = store.getItem('AccountID');
var b = store.getItem('CityID')
我怎樣才能使它所以我refreshGrid( 「頁」);函數只能運行,如果這兩個都設置爲值?
如果要檢查它們是否明確地null
,只是數值比較null
:
if(a !== null && b !== null) {
//Neither is null
}
如果要存儲在那裏,值應該始終評估爲true
,你可以跳過平等檢查(如果沒有被存儲在特定的存儲項目,這仍然會失敗,因爲null
是falsy):
if(a && b) {
//Neither is falsy
}
你FUNC頂部使用guard condition重刑:
您可以使用此:
var a; var b;
if (a == null && b == null) {
//a and b are null
}
OR
if (!a && !b) {
//a and b are null, undefined, 0 or false
}
對於詹姆斯:警報(空==未定義)//返回true
希望這有助於。
第一個不檢查「a和b是否爲空」,它只是檢查它們評估爲false。它們可以是'null','undefined','0'或'false'等等。 –
你是對的另外我還補充了更多。 – AlphaMale
我認爲以下代碼就是您要找的。
if(!!a && !!b){
alert("this is not null")
}else{
alert("this is null")
}
支票(一===空)&&(二===空) – Hoque