2

This post inspired me. I've made some tests.

console.log( false, 5 ); prints false 5 , and it's ok.

console.log( ( false, 5 ) ); prints 5. Now we know that it's ok too because ( false, 5 ) returns 5.

But why does console.log( false, {}, 5 ); print false Object {} 5?

Also console.log( ( false, {}, 5 ) ); and even console.log( ( false, { i:0 }, 5 ) ); both prints 5. Why is 5 is preferred to {}?

You can see here: http://jsfiddle.net/3uUwY/

4 Answers 4

6

The comma operator always returns the last element, which is 5.

Sign up to request clarification or add additional context in comments.

2 Comments

oh , i thought it returns first one that is not undefined or null
That would have been a || b || c.
1

When using brackets you are forcing Javascript to evaluate that expression.

console.log(a, b, c); // 3 parameters, the function prints a, b and c

console.log((a, b, c)); // 1 parameter. It prints the result of 
                        // evaluating (a, b, c) and, as it's said 
                        // in the other answer, it returns the last element
                        // of the expression.

1 Comment

Thank you , now i know that it is always last element
1

By putting brakets you make only one argument to console.log. So following

console.log( false, 5 ); // here you are using log function with 2 argumetns 

And here

console.log( ( false, { i:0 }, 5 ) ); // here is only one argument.

Inside a brakets you are using comma operator.

And the comma operator always returns last expression.

So you could rewrite your expression like this:

var x = ( false, { i:0 }, 5 ); // x is 5 here
console.log( x );

5 Comments

Declaring multiple variables by separating them with a comma is unrelated to the comma operator.
@Juhana And what operator is using there?
It's not an operator, it's just part of the variable-declaring syntax.
When used to separate elements like when declaring variables or separating arguments in a function, it's not an operator. It's only an operator when it's used as such. (If you don't believe me, look up what the definition of an operator is.)
@hazzik The example at the bottom of the page you linked to says the comma used when declaring multiple variables is not an operator. But something like i++, j-- is.
1

By putting brakets you make only one argument to console.log. So following

console.log( false, 5 ); // here you are using log function with 2 argumetns 

And here

console.log( ( false, { i:0 }, 5 ) ); // here is only one argument.

Inside a brakets you are using comma operator.

And the comma operator always returns last expression.

So you could rewrite your expression like this:

var x = ( false, { i:0 }, 5 ); // x is 5 here
console.log( x );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.