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

96
Views
Elegant way to return from try...catch - Javascript

Elegant way to return null. Don't want to return it twice.

Option 1:

function readSessionStorage(key) {
    try {
        if (typeof window !== 'undefined') {
            return JSON.parse(window.sessionStorage.getItem(key));
        }
        return null;
    } catch {
        return null;
    }
}

Option 2:

function readSessionStorage(key) {
    try {
        return JSON.parse(window.sessionStorage.getItem(key));
    } catch {
        return null;
    }
}

Option 3: If we pick this option, why should we do this?

function readSessionStorage(key) {
    try {
        if (typeof window !== 'undefined') {
            return JSON.parse(window.sessionStorage.getItem(key));
        }
    } catch {}
    return null;
}

Why do I need to do this?

I'm getting DOMException if I try to get window.sessionStorage, and hence I need to use try...catch.

function readSessionStorage(key) {
    if (typeof window !== 'undefined' || !window.sessionStorage) {
        return JSON.parse(window.sessionStorage.getItem(key));
    }
    return null;
}

Original Code:

function readSessionStorage(key) {
    if (typeof window !== 'undefined') {
        return JSON.parse(window.sessionStorage.getItem(key));
    }
    return null;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well, that's odd. No response in seven months? Well, I was looking for something like this myself, and couldn't find an eloquent solution. I created my own.


Edit: upon further work, I found that the null safety operators do exist in JS! Yay! The function below is still quite helpful, perhaps more so now since one can use the safety operator to get to the precise value desired, then handle it correctly.


Consider using a function similar to this:

const defineCheck = (item, defaultVal = '') => { 
    try {
        return (item !== null && item !== undefined) ? item : defaultVal;
    } catch {
        return defaultVal;
    }
}

This might not exactly fit your needs, but you can make your function however you want. This will keep the clutter out of your important business or UI logic, so you and others can focus on what is important.

Sample usage (if account is known to have an expected value):

tempForm.ExpirationDate = defineCheck(account.expirationDate);
tempForm.LicenseType = defineCheck(account.licenseType, 'Nada');

With null safety:

tempForm.ExpirationDate = defineCheck(account?.license?.expireDate);
tempForm.LicenseType = defineCheck(account?.license?.licenseType, 'Nada');

I hope you and others find this useful, or better yet, share a much better way to deal with this problem. I'd hoped the C# null safety check operator made its way to JS, but no luck (e.g., "account?.license?.licenseType ?? 'Nada'")

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!