Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8576127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:51:20+00:00 2026-06-11T19:51:20+00:00

I am reusing a function in my javascript for two places. Within this method

  • 0

I am reusing a function in my javascript for two places.

Within this method I make a call and retrieve a json object (data)

data[0].fields['status']

In one of the cases I know that the key ‘status’ won’t be there and I need to use a hardcoded value, but since the code is shared for both cases, I need to capture this case.

Is there a get method to check safely for the key and if it doesn’t exist I would use the hardcoded value?

data[0].fields.get('status') ?

Or is there is a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:51:21+00:00Added an answer on June 11, 2026 at 7:51 pm

    Use the in operator to check whether a property is existent on an object:

    if ('status' in data[0].fields) …
    

    You also could use a simple boolean evaluation if your potential value is truthy, because the undefined evaluates to false. You can use that even in a short-circuit evaluation:

    return data[0].fields.status || defaultstate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a big JavaScript object literal I am reusing with very few modifications
I'm re-using the following JavaScript to make a object fade out when clicked, but
I have a javascript that calculates the percentage from two fields (retail and network)
I'm reusing a created Object just to change a Date and the ordinal value,
I have two functions here say animation1 and load1 they work like that: function
I've got a question about reusing table data but a view won't work in
This may seem simple to some but I am less experienced with JavaScript. I
I've made a small function that I'll be reusing several times in an application.
So, I have the setup like this (in Express): app.get('/mycall1', function(req,res) { res.send('Good'); });
I'm working through some javascript examples, and I just did this one: <!DOCTYPE html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.