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

126
Views
How to shows nothing if a list of value is undefined in Javascript?

Sorry for this, maybe is an easy and basic question

I'm getting results from an endpoint but some of these values could be empty, then on my screen shows "undefined"

How can I check in a clean code if some of the value is undefined then show nothing?

myvalues() {
  if (!this.myendpointdata) {
    return '';
  }
  const { value1, value2, value3, value4 } =
    this.myendpoindata.master.values;
  return `${value1} ${value2} ${value3} ${value4}`;
}

This is my code. And, for example, value2 shows undefined

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

0

You can set empty strings if your values are undefined

myvalues() {
  if (!this.myendpointdata) {
    return '';
  }
  const { value1, value2, value3, value4 } =
    this.myendpoindata.master.values;
  return `${value1 || ""} ${value2 || ""} ${value2 || ""} ${value4 || ""}`;
}

You also can loop through values and filter not undefined values out for display

myvalues() {
  if (!this.myendpointdata) {
    return '';
  }
  const { value1, value2, value3, value4 } = this.myendpoindata.master.values;
  const values = Object.values({ value1, value2, value3, value4 });
  return values.filter(value => !value).join(" ");
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use ternary operations:

return `${value1 ? value1 : ''} ${value2 ? value2 : ''} ${value3 ? value3 : ''} ${value4 ? value4 : ''}`;
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!