Search code examples
javascriptternary

Weird ternary operator Javascript


I'm trying to understand how ternary operators work and I came across this example:

b.d >= mystr.length && (function1(b, a), a=0);

What does && mean? is it used like an AND operator? how does this translate to regular statement? What does the coma before a=0 mean? Thanks!


Solution

  • && is the AND operator. If the left of it is true, it's evaluate the right side (and return it). The , is the comma operator. (The comma operator evaluate both its sides, left to right, and return the right side). So this code is like:

    if (b.d>=mystr.lengh) {
     function1(b,a);
     a=0;
    }
    

    (Except that your code return 0)

    (My native language is C, so maybe I'm wrong, but I think that in this case, javascript work like C)