简体   繁体   中英

Why does “null” increase length of an array even though it represents empty value?

When "null" is included in the items in an array it counts in the length of the array. But "null" should mean no value. Then why does it happen?

 var myArray = ["pizza", "burger", null]; var test = function (input) { return input.length; }; document.write(test(myArray)); 

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.

It is not completely absent, that is intentional absence, which means that someone wants a null value at the position and use/update it later.

null have a unique meaning

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/null

It's because null is also an value. null represents the intentional absence of any object value

See : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/null

In Layman's term, It's an empty box which is supposed to contain something. When you are adding null to an array, consider as you are adding an empty box in a larger container.

因为null是一个引用,但它没有引用,但它仍然占用空间

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