简体   繁体   中英

How does it work push empty array into an empty array

var ar = []; 
console.log(ar.push([]));   // 1 

I expected the log to be [[]] , but it show me 1. What is going on above the code.

According to MDN , the Array#push returns:

The new length property of the object upon which the method was called.

So, in your case, as you are pushing an element to an empty array - which makes the length 1 - hence the result.

In case you wish to confirm:

 // Empty array. var arr = []; // Push an item: result = 1 console.log(arr.push([])); // Push another item: result = 2 console.log(arr.push([]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM