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

245
Views
how to return a value to variable if varible path returns undefined?

I have a variable and I want to return the value 0 if the original path returns as undefined.

I tried to do this with an IF statement inside the variable but this doesn't work.

Thank you for any help

const balance = wallet.balance if (balance === undefined || balance === null) {  
console.log("Balance is null or undefined"); 
balance = 0;
};

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

0

The easiest way would be using the nullish coalescing operator ??:

const balance = wallet.balance ?? 0

Something closer to what you already wrote would be using a ternary:

const balance = wallet.balance == null ? 0 : wallet.balance

(Note that wallet.balance == null with loose comparison is a shorter way to check for both null and undefined at once.)

Of course you could also write an if, but then it would have be at its own line - and the variable would have to be non-const so it can be modified:

let balance = wallet.balance
if (balance == null) {  
  console.log("Balance is null or undefined");
  balance = 0
}
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!