2017-08-04 11 views
-1

数値がnullでないことを確認する必要があります。変数をチェックする方法はnullではなく、空ではありませんが、ゼロはjavascriptで渡す必要があります

しかし、それは0 1 2のように数値を渡す必要があるなど

var xxx=0; 
if(xxx){ 
// for 0 not coming here 
}else{ 
// All empty,undefined,'' should go here --- 
} 
+0

は "空" とは何か、なぜそれが未定義よりも違うのですか? – h2ooooooo

+1

'if(isFinite(xxx))' – tymeJV

+0

'0'はJavascriptの条件で' false'とみなされます – Vandesh

答えて

3

あなたはtruthyまたはゼロの値をチェックすることができます。

if (x || x === 0){ 
    // 0, 1, 2, 3 
} else { 
    // null, undefined, '' 
} 
1

チェックこのアウト:

function is_number(val){ 
 
if (val || val === 0){ 
 
    return true; 
 
} else { 
 
    return false; 
 
} 
 

 
} 
 
console.log(is_number(0)) 
 
console.log(is_number(1)) 
 
console.log(is_number(null)) 
 
console.log(is_number(""))

関連する問題