Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
JavaScript check for value including 0

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 :/

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

For type check you have:

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

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

isNaN()/Number.isNaN(): https://developer.mozilla.org/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
about 4 years ago · Juan Pablo Isaza Report

0

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;
}
about 4 years ago · Juan Pablo Isaza Report

0

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))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!