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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:39:05+00:00 2026-06-19T03:39:05+00:00

I am interested in the practical application of declaring variables using && like this:

  • 0

I am interested in the practical application of declaring variables using && like this:

var x = undefined && 4;

// Evaluate to the first falsey value
// or else the last value.

eval(x);
// undefined

I understand how the value is evaluated (see this SO answer). I also understand its sister || (see here for a great description) and why it would be useful to declare a variable with the following expression:

// Some other variable
var y;

var x = y || 4;

// Evaluate to the first truthy value
// or else the last value.

Practically: Use the first value unless that first value is falsey; if so, use the last value. We can demonstrate this characteristic of || in the browser console:

> null || 4
  4
> 4 || null
  4

> null || undefined
  undefined
> undefined || null
  null

> true || 4
  true
> 4 || true
  4

As for &&:

> null && 4
  null
> 4 && null
  null

> null && undefined
  null
> undefined && null
  undefined

> true && 4
  4
> 4 && true
  true

Should we take this to mean: Use the first value unless that first value is truthy; if so, use the last value?

I’m interested in using coding shortcuts to minimize the use of conditional statements, and I wonder if I might be able to use this one somehow.

I found an example of this coding method in line 472 of the jQuery core source:

scripts = !keepScripts && [];

So the question is this: Can anyone describe a good context for using && in a javascript variable declaration? Do you consider it to be bad practice?

Thanks!

  • 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-19T03:39:06+00:00Added an answer on June 19, 2026 at 3:39 am

    In general, you should only only use “shortcuts” like this if it makes the code more readable for the typical JavaScript programmer than the alternative.

    When thinking about what is more readable, and less surprising, consider that

    var foo;
    if(bar) {
        foo=[];
    }
    

    and

    var foo = bar && [];
    

    are not the same. For instance, if bar is NaN, then foo will then be NaN in the later case, which might be a bit of a head-scratcher later on.

    Since there are tools to optimize/minimize JavaScript, you should focus on readability of your code, which is not always the same thing as brevity.

    Lets say you have several such repetitive initializations in a row, all dependent on different variables (so that they couldn’t be wrapped into a single conditional), but following the same logical formula. In this case, once the reader had mentally parsed the meaning of the formula, they could quickly scan all the instances and see the differences between each one. In this case, instead of relying on a convention that most JavaScript programmers are familiar with (such as var foo = some_opt || {}), you are creating a localized convention, that the reader will have to learn just for this file. Even in this case, I’d advise some careful consideration, its probably not worth it.

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

Sidebar

Related Questions

I'd like to add persistence to my Swing-based application; and this is the first
I am interested to know the performance impacts of using MySQL as a key-value
I'm creating a multi-user/company web application in PHP & MySQL. I'm interested to know
I am interested in doing this C code in Java: // sets n's ith
First of all, I'm attempting to use faux namespaces in my JavaScript program like
I am interested in writing an application that will take in an excel document
I want to add a menu to my First Ever Rails Application. Nothing too
make C++ functions or structs, classes (using meta-programming) determining maximum value for signed and
We are interested in implementing something into our ASP.NET web application that will send
In a typical iOS application, where should one put application wide objects? I'd like

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.