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

157
Views
Boolean($var) VS $val ? true : false

Q. Which is more READABLE ?

const emptyValue = "";

Boolean(emptyValue);
// false

VS

const emptyValue = "";

emptyValue ? true : false;
// false

This is a very common case in which you want the flow changes according to a specific value of a variable. I know this has almost no difference in a matter of performance, but I'm really curious about the option of the community in a matter of READABILITY.

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

0

In my opinion is more readable the second, namely:

const emptyValue = "";

emptyValue ? true : false;

but the javascript way to convert in a Boolean type is:

const emptyValue = "";

!!emptyValue;

! calls Boolean() behind the hood, thereby is called twice and is less efficient than the single Boolean() solution, but I repeat is the javascript way.

about 4 years ago · Juan Pablo Isaza Report

0

I prefer just to test the variable name

It works for ALL except 0 if you need to test a number - for that you can use !! Double Not

It is possible to use a couple of NOT operators in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value

const emptyValue = "";
let undefinedValue;
const zero = 0;
const nullValue = null
const definedVariable = "Hello"

console.log(definedVariable,emptyValue,undefinedValue,zero,nullValue)

console.log(Boolean(definedVariable), Boolean(emptyValue),Boolean(undefinedValue),Boolean(zero),Boolean(nullValue))
console.log(!!definedVariable ,!!emptyValue,!!undefinedValue,!!zero,!!nullValue)

if (definedVariable) console.log("definedVariable is truthy");
if (!emptyValue) console.log("emptyValue is falsy");
if (!undefinedValue) console.log("undefinedValue is falsy");
if (!zero) console.log("zero is falsy"); 
if (!!zero) console.log("zero is not falsy with !!"); 
if (!nullValue) console.log("nullValue is falsy");

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!