Current Size:
Chakra BreakPoints
base0px
sm480px
md768px
lg992px
xl1280px
2xl1536px
Current Height:Width
widthpx
heightpx

Subjects

Methods for generating empty and stubbed js arrays

Date Created: 2024/02/10

Last Update: 2024/02/10

#javascript #general #cheatsheet #notes
new Array(3).fill(0)

Push Method

const LEN = 3
const arr = []
for (let i=0; i<LEN; i++){
arr.push(0)
}
// [0,0,0]

Mapping method

Personally I think the mapping methods are the cleanest and most versatile

const arr = Array.from({length: 3}, () => 0)
// [0, 0, 0]
const arr = Array.from({length: 3}, (x,i) => i)
// [0, 1, 2]

More Notes

All Notes
HomeProjects

Links

Home Articles Notes Projects About Style Guide Site Credits

Contact

 [email protected]

Location

🌎 Earth