Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Js) what will be the simplest way to check null or '' as false but not 0?

I usually used if(value) to check value is null or not. But now I know false or 0 can be false. So I used if(value == null) but in this time, that null doesn't contain '' like. I want to check that 0, false(have specific value) as true, and I want to check that '', undefined, null as false What will be good way to check these values simply? Is there something to notice bout like this more?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Maybe this small helper function helps? Play with it in 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 Relatório

0

Just check for the possible values normally.

const isFalsy = param => (param === null) || (param === "") || (typeof param === "undefined")

isFalsy(null); // true
isFalsy(0); // false

I want to check that 0, false(have specific value) as true.

if(!isFalsy(value))
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda