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

153
Views
Js) ¿cuál será la forma más sencilla de verificar nulo o '' como falso pero no 0?

Usualmente usé if(value) para verificar que el valor sea nulo o no. Pero ahora sé false o 0 puede ser falso. Entonces usé if(value == null) pero en este momento, ese nulo no contiene '' like. Quiero verificar que 0 , false (tiene un valor específico) como verdadero, y quiero verificar que '' , undefined , null como falso ¿Cuál sería una buena manera de verificar estos valores simplemente? ¿Hay algo que notar más sobre esto?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

¿Tal vez esta pequeña función auxiliar ayude? Juega con él en Stackblitz .

 test(); test(42); test(Infinity); test(null); test(0); test({}); test(new Map()); function test(someValue) { if (hasValue(someValue)) { return console.log(`${someValue} is truthy`); } return console.log(`${someValue} is falsy`); } function hasValue(v) { // Objects are always considered values if (v instanceof Object) { return true; } const falsies2Check = [false, 0, '', null, undefined, Infinity]; return falsies2Check.find(f => f === v || isNaN(v)) === undefined ? true : false; }

about 4 years ago · Santiago Trujillo Report

0

Simplemente verifique los valores posibles normalmente.

 const isFalsy = param => (param === null) || (param === "") || (typeof param === "undefined") isFalsy(null); // true isFalsy(0); // false

Quiero verificar que 0 , false (tiene un valor específico) como verdadero.

 if(!isFalsy(value))
about 4 years ago · Santiago Trujillo 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!