Return the length of an array:
const players = ["Banana", "Rohit", "Virat", "Mango"]; players.length;
1) Dense arrays
let players = ['sachin', 'dhoni', 'blue']; console.log(players.length); // 3
The following adds one more element to the players array:
players.push('yellow'); console.log(players.length); // 4
Now, the length property of the players array is four.
When you empty the players array, its length is zero:
players = ; console.log(players.length); // 0
2) Sparse arrays
numbers = 100; console.log(numbers.length); // 11
1) Empty an array
const players = ['Virat', 'Rohit', 'Rahul']; players.length = 0; console.log(players); // 
2) Remove elements
const players = ['Virat', 'Rohit', 'Rahul']; players.length = 2; console.log(players); // [ 'Virat', 'Rohit' ]
3) Make array sparse
const players = ['Virat', 'Rohit', 'Rahul']; players.length = 5; console.log(players); // [ 'Virat', 'Rohit', 'Rahul', <2 empty items> ]
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.