Truncate an Array by setting its length

Today I Learned that in JavaScript an Array can be truncated by simply setting its length 🤯

Here is an example:

const myArray = [1, 2, 4, 8, 16, 32, 64, 128]
myArray.length = 4

console.log(myArray)
// [1,2,4,8]

Comments

No comments

It's a bit quiet here. Why not be the first to post something?

Subscribe to our newsletter

No spam. Just simple and infrequent platform updates when something big happens.

© 2024 TechTalks.io Limited. All rights reserved.