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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:56:53+00:00 2026-05-23T16:56:53+00:00

Is this valid JavaScript? I saw an example where someone used commas in the

  • 0

Is this valid JavaScript? I saw an example where someone used commas in the ternary operator conditions, and it was marked as an error in my editor, and the example didn’t run in Chrome. However, it did run in Firefox. Once I converted all the ternary statements to if/else statements, the app ran on Chrome.

a!==b ? (a=1, b=2) : (a=2, b=1)

Edit:

This is the actual statement in the code:

a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0
  • 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-05-23T16:56:54+00:00Added an answer on May 23, 2026 at 4:56 pm

    Yes, it’s valid, and it runs fine in Chrome:

    var a, b, c;
    
    a = 6;
    b = 7;
    c = a !== b ? (a = 1, b = 2) : (a = 2, b = 1);
    console.log("a = " + a);
    console.log("b = " + b);
    console.log("c = " + c);

    I’m not saying it’s a remotely good idea in code humans are meant to read. 🙂 I expect jamietre is correct in the comments when he/she says it looks like the result of minification.

    The comma operator is a binary operator (an operator accepting two operands). It evaluates its left-hand operand (thus causing any side-effects it has, such as assignment), throws that result away, then evalutes its right-hand operand (thus causing its side-effects if any) and takes that result as its result value. If you have multiple comma operators in a row, the overall expression is evaluated in order, left-to-right, with the final result being the value resulting from the right-most operand evaluation.

    And of course, you know the conditional operator (a ternary operator — one accepting three operands) is used to pick one of two sub-expressions to evaluate, on the basis of an initial expression.

    So that line is very…expressive…what with a total of seven* different expressions inside it.

    So in that example, the result of the overall expression is 2 if a !== b initially, or 1 if a === b initially, with the side-effects of setting a and b.

    It’s the side effects that make it, in my view, a questionable choice. And of course, there’s no reason to use the comma operator if the left-hand operand doesn’t have side effects.


    * Yes, seven of ’em packed into that overall ternary:

    • a !== b
    • the first comma expression
    • a = 1
    • b = 2
    • the second comma expression
    • a = 2
    • b = 1

    Re your edit with the actual statement, that one works too:

    function test(a) {
        var b = 7,
            d = 1,
            e = 2,
            f = 3,
            g = 4,
            h = 5,
            i = 6;
        
        a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0;
        
        console.log("a = " + a);
        console.log("b = " + b);
        console.log("d = " + d);
        console.log("e = " + e);
        console.log("f = " + f);
        console.log("g = " + g);
        console.log("h = " + h);
        console.log("i = " + i);
    }
    
    test(0);
    test(1);
    .as-console-wrapper {
      max-height: 100% !important;
    }

    But wow, I hope this is minified, because if a person wrote that, they must really have a thing against anyone who’s supposed to maintain it later… 😉

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

Sidebar

Related Questions

It is valid JavaScript to write something like this: function example(x) { Here is
is this a valid JQuery usage pattern to : <script type=text/javascript> $(body).prepend('<input type=hidden id=error
Is this a valid url parameter in jquery.ajax(), <script type=text/javascript> $(document).ready(function() { getRecordspage(); });
This regex isn't valid in JavaScript. It says there is an invalid group. Can
I created a javascript document and I want to make this JSlint valid. http://pastebin.com/GvZLyNbV
Lately I saw working code-blocks like this: <script type=text/javascript src=//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js></script> And according to RFC
Is this valid Javascript syntax? What does it do? Parser.prototype = { // ...
I am new to javascript, but is this valid? <script type=text/javascript> if (condition()) {
Is this valid jQuery? Or have I mixed it up with regular JavaScript too
This is valid and returns the string 10 in JavaScript ( more examples here

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.