denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.75k stars 2.55k forks source link

adding ```{}{}``` is undefined section #166

Closed hsl0 closed 3 years ago

hsl0 commented 3 years ago

Continues from #165

Thanks for @libook to suggesting idea of writing explanation,

{}{} is undefined

Write them in console. They will return the value that defined in last object.

{}{}; // undefined
{}{}{}; // undefined
{}{}{}{}; // undefined
{foo: 'bar'}{}; // 'bar'
{}{foo: 'bar'}; // 'bar'
{}{foo: 'bar'}{}; // 'bar'
{a: 'b'}{c:' d'}{}; // 'd'
{a: 'b', c: 'd'}{}; // SyntaxError: Unexpected token ':'
({}{}); // SyntaxError: Unexpected token '{'

💡 Explanation:

When inspecting each {}, they returns undefined. If you inspect {foo: 'bar'}{}, you will find {foo: 'bar'} is 'bar'.

There are 2 meaning for {}: object and block. For example, the {} in ()=>{} means block. So we need to use ()=>({}) to return an object.

Let's use {foo: 'bar'} as a block. Write this snippet in your console:

if(true) {foo: 'bar'} // 'bar'

Surprisingly, it behaviors the same! You can guess here that {foo: 'bar'}{} is a block.

denysdovhan commented 3 years ago

Sorry for waiting! I'm trying to maintain a live/work balance and been out of my projects for a while.

github-actions[bot] commented 3 years ago

:tada: This PR is included in version 1.16.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: