JavaScript check for value including 0 - Stack Overflow

In javascript I need to check for a value in an if statement if it exists. The thing is 0 can be one of

In javascript I need to check for a value in an if statement if it exists. The thing is 0 can be one of the accepted values. so when I do

if(!val) {
   return true
}

return false

The thing is Javascript evaluates !0 = false

here are test cases what I want:

val = 0 // true
val = 91 // true
val = null // false
val = undefined = false

Basically check check for null but include 0. Not sure how to do this :/

In javascript I need to check for a value in an if statement if it exists. The thing is 0 can be one of the accepted values. so when I do

if(!val) {
   return true
}

return false

The thing is Javascript evaluates !0 = false

here are test cases what I want:

val = 0 // true
val = 91 // true
val = null // false
val = undefined = false

Basically check check for null but include 0. Not sure how to do this :/

Share Improve this question asked Sep 16, 2021 at 20:39 ousmane784ousmane784 4661 gold badge11 silver badges22 bronze badges 8
  • 1 What about val != null? – evolutionxbox Commented Sep 16, 2021 at 20:42
  • 1 (val === null || val === undefined) – Daniel Beck Commented Sep 16, 2021 at 20:42
  • @DanielBeck is that the same as !(val !== null)? – evolutionxbox Commented Sep 16, 2021 at 20:45
  • 1 Does this answer your question? JavaScript check if variable exists (is defined/initialized) – esqew Commented Sep 16, 2021 at 20:45
  • 1 if(!val || val === 0){...} – user15456842 Commented Sep 16, 2021 at 20:54
 |  Show 3 more ments

4 Answers 4

Reset to default 2

0 is falsy so !0 is true in JavaScript, but to meet your test cases I think you want truthy values to return true, and you need to handle the special case for 0. One way to do it:

function test(val) {
  if (val || val === 0) {
    return true
  }
  return false
}

console.log(test(0));
console.log(test(91));
console.log(test(null));
console.log(test(undefined));

You could also leverage Javascript's weak equivalence operator and use != null which covers null and undefined as below:

function test(val) {
  if (val != null) {
    return true
  }
  return false
}

console.log(test(0));
console.log(test(91));
console.log(test(null));
console.log(test(undefined));

For type check you have:

typeOf: // note: typeof [] = object and typeof {} = object https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/typeof

Array.isArray(): https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

isNaN()/Number.isNaN(): https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

Is one of those what you're looking for?

!0 = true // in js. just tried it on MDN

Check for undefined and null:

function isValNumber(val) {
  if (val === undefined || val === null) {
    return false
  }
  return true
}

console.log(isValNumber(0))
console.log(isValNumber(91))
console.log(isValNumber(null))
console.log(isValNumber(undefined))

I think you need to be checking for null & undefined in your conditional statement. That way, anything that is 0 or greater will return true. You also need to check the typeOf() for undefined because you undefined is not the value of the variable.

if (typeof(val) != 'undefined' && val != null && !(val < 0)){
    return true;
}else{
    return false;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744895445a4599668.html

相关推荐

  • JavaScript check for value including 0 - Stack Overflow

    In javascript I need to check for a value in an if statement if it exists. The thing is 0 can be one of

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信