Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - brackets in console.log method issue

Tags:

javascript

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/

like image 971
Ivan Chernykh Avatar asked Dec 11 '25 21:12

Ivan Chernykh


1 Answers

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

like image 159
Denys Séguret Avatar answered Dec 13 '25 10:12

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!