Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The length property of an array
object can be used to get or set the length of an array. As shown above,
setting the length higher than the actual number of values contained in
the array will add undefined values to
the array. What you might not expect is that you can actually remove
values from an array by setting the length value to a number less than the
number of values contained in the array.
<!DOCTYPE html><html lang="en"><body><script> var myArray = ['blue', 'green', 'orange', 'red']; console.log(myArray.length); // logs 4 myArray.length = 99; console.log(myArray.length); /* logs 99, remember we set the length, not an index */ myArray.length = 1; // removed all but one value, so index [1] is gone! console.log(myArray[1]); // logs undefined console.log(myArray); // logs '["blue"]' </script></body></html>