JavaScript Array length
JavaScript array length property is used to set or return the number of elements in an array.
let a = ["js", "html", "gfg"];
console.log(a.length);
Output
3
Setting the Length of an Array
The length property can also be used to set the length of an array. It allows you to truncate or extend the array.
Truncating the Array
let a = ["js", "html", "gfg"];
a.length = 2;
console.log(a);
Output
[ 'js', 'html' ]
Extending an Array
let a = ["js", "html", "gfg"];
a.length = 5;
console.log(a);
Output
[ 'js', 'html', 'gfg', <2 empty items> ]
size() vs length for finding length
Underscore.js defines a size method which actually returns the length of an object or array, but size is not a native method. So it is always recommended to use length property.
String Length vs Array Length Property
String Length properly works same way, but we cannot modify length of a string as strings are immutable.
let a = ["js", "css", "html"];
a.length = 2; // Changes length of array
console.log(a);
let s = "geeksforgeeks"
s.length = 3; // Has no effect on string
console.log(s);
Output
[ 'js', 'css' ] geeksforgeeks

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
