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

  • SEARCH
  • Home
  • 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 8944805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:06:19+00:00 2026-06-15T12:06:19+00:00

I like to have somewhat clean code: var currentVar = aBigObject[‘Key1’][‘Key2’][‘Key3’]; generalValues.push(((!currentVar) ? 0

  • 0

I like to have somewhat clean code:

var currentVar = aBigObject['Key1']['Key2']['Key3'];
generalValues.push(((!currentVar) ? 0 : currentVar));

The alternative (I think) is this:

if (!aBigObject['Key1']['Key2][Key3']) generalValues.push(0);
else generalValues.push(aBigObject['Key1']['Key2']['Key3']);

To me, the second is unnecessarily long and difficult to read. My question is, is it really safe/good practice to set a variable to something that can potentially be undefined?

  • 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-15T12:06:20+00:00Added an answer on June 15, 2026 at 12:06 pm

    The former to me is unnecessarily long and difficult to read. My question is, is it really safe/good practice to set a variable to something that can potentially be undefined?

    It’s perfectly safe in that it’s not going to cause an error. It also means that you don’t have to do all of those lookups again.

    Your alternative also seems to make little sense (and certainly isn’t doing the same thing as your first code snippet), as it has a totally different behavior when aBigObject['Key1']['Key2][Key3'] isn’t falsey (assigning it to itself rather than pushing it on generalValues). (The edit to the question fixes this.)

    Your first example certainly has lots of unnecessary parens and can be written:

    var currentVar = aBigObject['Key1']['Key2']['Key3'];
    generalValues.push(!currentVar ? 0 : currentVar);
    

    ..and could possibly be better written as

    generalValues.push(aBigObject['Key1']['Key2']['Key3'] || 0);
    

    Your general point seems to be, “Why is the first snippet using currentVar?” And the answer is that property lookups are not free. So once you’ve done the lookup of Key1 on aBigObject, and Key2 on the result, and Key3 on the result of that, you remember and reuse it rather than looking it up again. JavaScript objects are hashmaps, lookups are cheap but they aren’t free. But in that specific case, you can do that another way (see above).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table structured somewhat like this: type name value ===================== 1
I have an app that behaves somewhat like a photo gallery. They choose an
Currently I have an algorithm which somewhat looks like web-spiders or file search systems
If I have an enum like this public enum Hungry { Somewhat, Very, CouldEatMySocks
I have a few hundred words in arrays in a plist, somewhat like below:
I have written an app that reads incoming chat(somewhat like an instant messenger), formats
I think I have something like programmer's OCD. I like my code to be
So in my project i would like have a nice treeview that has images.
I have like 400 records on my database, suppose this is the first version
I have a table like: I have to order the rows in function of

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.