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

221
Views
How to check for falsey string value when concatenating?

In the following examples both ex1 & ex2 variables will render an empty string with a space and not a falsy value. Whereas ex3 will be falsy and render the right side of the || operator. How can I test for empty string in the first two examples without doing an if statement?

let var1 = '';
let var2 = '';
let ex1 = `${var1} ${var2}` || "Is Falsy";
let ex2 = var1 + ' ' + var2 || "Is Falsy";
let ex3 = var1 || "Is Falsy";

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

0

i think this will work for you :

let ex2 = var1 || var2 || "Is Falsy";

about 4 years ago · Juan Pablo Isaza Report

0

Are ternary operators acceptable?

let ex1 = var1 && var2 ? `${var1} ${var2}` : "Is Falsy";
about 4 years ago · Juan Pablo Isaza Report

0

If you are okay with using conditional operators, this can be done with the help of trim() :

let var1 = '';
let var2 = '';
let ex1 = (`${var1} ${var2}`).trim()/length > 0 ? `${var1} ${var2}` : "Is Falsy";
let ex2 = (var1 + ' ' + var2).trim().length > 0 ? var1 + ' ' + var2 : "Is Falsy";
let ex3 = var1 || "Is Falsy";
console.log(ex1);
console.log(ex2);
console.log(ex3);

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!