Javascript - brackets in console.log method issue - Stack Overflow

This post inspired me. I've made some tests.console.log( false, 5 ); prints false 5 , and it'

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: /

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/3uUwY/

Share Improve this question edited May 23, 2017 at 11:52 CommunityBot 11 silver badge asked Sep 1, 2013 at 10:44 Ivan ChernykhIvan Chernykh 42.2k13 gold badges136 silver badges148 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

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

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.

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 ma operator.

And the ma 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 );

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 ma operator.

And the ma 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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744214591a4563492.html

相关推荐

  • Javascript - brackets in console.log method issue - Stack Overflow

    This post inspired me. I've made some tests.console.log( false, 5 ); prints false 5 , and it'

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信