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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:30:59+00:00 2026-06-02T18:30:59+00:00

While I was killing time looking up Javascript shorthand patterns, I came across an

  • 0

While I was killing time looking up Javascript shorthand patterns, I came across an interesting way of expressing an if statement here. The following works:

​var var1 = 5;
var var2 = 1;

var1 == 5 && var2++;

I think that’s a totally cool (and shorter, cleaner) way of writing an if statement that only needs to do one operation. However, I ran into an issue with the following code as soon as I tried something else with it:

​var var1 = 5;
var var2 = 1;

var1 == 5 && var2 = 2;

Instead of working like the first code snippet, my test throws an error:

Uncaught ReferenceError: Invalid left-hand side in assignment

Why doesn’t this work, and why is the left-hand side of my statement being called out as incorrect as opposed to the right hand?

  • 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-02T18:31:01+00:00Added an answer on June 2, 2026 at 6:31 pm

    This doesn’t work because the && operator has higher precedence than the = operator. In other words, your code is interpreted like this:

    (var1 == 5 && var2) = 2; 
    

    If you manually put parentheses around the assignment, it will be interpreted correctly.

    var1 == 5 && (var2 = 2); 
    

    Other people will reprimand you for writing code like this, saying that is a bad idea because it makes the intent of the code harder to read–which is true, especially for people unfamiliar with this idiom–but it is important to try it out and experiment with things like this to see for yourself what happens. You’ve already encountered one of the annoying things about this approach.

    Anyway, in cases like this, I personally prefer to use single line if statements, like this

    if(condition) statement;
    

    Although I’m sure others will tell you that that’s bad style because you have to manually add brackets when you need more statements, this hasn’t really bit me and IMO it’s cleaner than using three lines just for a simple condition, like you said.

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

Sidebar

Related Questions

While doing some JavaScript performance tests I came up with the following piece of
I need to find a way to stop a while statement when a messagebox
While reading through another question here, on creating a URL shortening service, it was
While running a thread program and repeatedly killing the main program using Ctrl +
I'm writing an application that spawns and kills a Chrome Browser. While I'm killing
Basically I have the following code: import multiprocessing import time class MyProcess(multiprocessing.Process): def __init__(self,
I have come across with a doubt regarding stopping (killing) a query: There is
Killing the processs while obtaining this information would be fine.
While editing .scm files it would be great if Vim would automatically put the
While my ASP.NET project uses version 3.5 of the framework , the event log

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.