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 9210901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:10:46+00:00 2026-06-18T01:10:46+00:00

The following is just a theoretical JavaScript question. I am curious if the following

  • 0

The following is just a theoretical JavaScript question. I am curious if the following can be converting into a single statement:

if(!window.foo){
  window.foo = [];
}
window.foo.push('bar');

everyone has probably written this code before, but can it be done in one line?
At first I thought something like this would work:

(window.foo || window.foo = []).push('bar');

but that doesn’t work because of an invalid assignment. Next I tried chaining something on the push, but that doesn’t work because push does not return the array.

Any thoughts on if this can be done in plain JavaScript?
(the result by the way should be that window.foo = ['bar'])

  • 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-18T01:10:47+00:00Added an answer on June 18, 2026 at 1:10 am

    You’ve got your assignment backwards*. It should be:

    (window.foo = window.foo || []).push('bar');
    

    The || operator in JavaScript does not return a boolean value. If the left hand side is truthy, it returns the left hand side, otherwise it returns the right hand side.

    a = a || [];
    

    is equivalent to

    a = a ? a : [];
    

    So an alternative way of writing the above is:

    (window.foo = window.foo ? window.foo : []).push('bar');
    

    * see comments for details

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

Sidebar

Related Questions

Time for a theoretical question I just ran across. The following code is valid
yesterday I just asked following question. How to customize tableView Section View - iPhone
I'm using the following code to just make a single UILabel on my cell.
Issue Using the following just simply doesn't work properly in -webkit- and -moz- browsers:
The following is just a snipplet of code from a large fxg file, which
I am just following a very simple microsoft tutorial, at the topic Create a
Why is the output of the following program just int3 and not int3&4 ?
The problem is purely academic, so the following is just an example; if I
I just fiddled following script. It's not working. <SCRIPT> function kin() { var kobj
I just use following code to add an image to my project, var paper

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.