masanos note

Array.prototype.length

2021-02-28
icon

Basic

const arr = []
console.log(arr.length)
> 0

const arr2 = ['banana', 'orange']
console.log(arr2.length)
> 2

const arr3 = ['banana', 'orange', '']
console.log(arr3.length)
> 3

Other

const arr4 = [null]
console.log(arr4.length)
> 1

const arr5 = [{}]
console.log(arr5.length)
> 1

const arr6 = [[]]
console.log(arr6.length)
> 1

const arr7 = [undefined]
> 1

String

const arr8 = ''
console.log(arr8.length)
> 0

const arr9 = 'aaa'
console.log(arr9.length)
> 3

Number

const arr10 = 1
console.log(arr10.length)
> undefined

Object

const arr11 = {}
console.log(arr11.length)
> undefined

null

const arr12 = null
console.log(arr12.length)
> Uncaught TypeError: Cannot read properties of null (reading'length') 

Boolean

const arr13 = true
console.log('arr13', arr13.length)
> undefined

const arr14 = false
console.log('arr14', arr14.length)
> undefined


Ref.

https://codepen.io/masanos/pen/wvXYLxp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length

A record of the development is left in a web note.
Masanos
I want to make the world I see happy. Little by little, I am preparing to start a business. Thank you for your support.
Buy Me A Coffee
Copyright© masanos All Rights Reserved.