freeCodeCamp/guide/portuguese/javascript/falsy-values/index.md

2.0 KiB

title localeTitle
Falsy Values Valores de Falsas

Descrição

Um valor falso é algo que é avaliado como FALSE, por exemplo, ao verificar uma variável. Existem apenas seis valores de falsey em JavaScript: undefined , null , NaN , 0 , "" (string vazia) e false claro.

Verificando valores falsos em variáveis

É possível verificar um valor falso em uma variável com uma condição simples:

if (!variable) { 
  // When the variable has a falsy value the condition is true. 
 } 

Exemplos gerais

var string = ""; // <-- falsy 
 
 var filledString = "some string in here"; // <-- truthy 
 
 var zero = 0; // <-- falsy 
 
 var numberGreaterThanZero // <-- truthy 
 
 var emptyArray = []; // <-- truthy, we'll explore more about this next 
 
 var emptyObject = {}; // <-- truthy 

Diversão com Arrays

if ([] == false) // <-- truthy, will run code in if-block 
 
 if ([]) // <-- truthy, will also run code in if-block 
 
 if ([] == true) // <-- falsy, will NOT run code in if-block 
 
 if (![]) // <-- falsy, will also NOT run code in if-block 

Embargo

Esteja ciente do tipo de dados ao avaliar um valor em um contexto booleano. Se o tipo de dados do valor for um número , a avaliação geral / falsa pode resultar em um resultado inesperado:

const match = { teamA: 0, teamB: 1 } 
 if (match.teamA) 
  // The following won't run due to the falsy evaluation 
  console.log('Team A: ' + match.teamA); 
 } 

Uma alternativa ao caso de uso acima é avaliar o valor usando typeof :

const match = { teamA: 0, teamB: 1 } 
 if (typeof match.teamA === 'number') 
  console.log('Team A: ' + match.teamA); 
 } 

Mais Informações