Example 1 — Example
Typical use.
JavaScript
const a = ["a", "b", "c"];
console.log(a.length);
a.length = 1;
console.log(a.join(","));How It Works
- Read. Three items → length 3.
- Truncate. Setting length to 1 keeps only "a".
The length property is the number of elements in an array (one more than the highest numeric index). Assigning a smaller length truncates the array.
const a = ["a", "b", "c"];
console.log(a.length);
a.length = 1;
console.log(a.join(","));Typical use.
const a = ["a", "b", "c"];
console.log(a.length);
a.length = 1;
console.log(a.join(","));CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful