Skip to content

Latest commit

 

History

History
74 lines (59 loc) · 2.02 KB

File metadata and controls

74 lines (59 loc) · 2.02 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
length Property (Array) (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
length Property
JavaScript
TypeScript
DHTML
Array object
Length property
length property (array)
e1c6377c-2e84-440a-9660-f1f512e4a938
11
mikejo5000
mikejo
ghogen

length Property (Array) (JavaScript)

Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.

Syntax

  
numVar = arrayObj.length   

Parameters

numVar
Required. Any number.

arrayObj
Required. Any Array object.

Remarks

In JavaScript arrays are sparse, and the elements in an array do not have to be contiguous. The length property is not necessarily the number of elements in the array. For example, in the following array definition, my_array.length contains 7, not 2:

var my_array = new Array( );  
my_array[0] = "Test";  
my_array[6] = "Another Test";  

If you make the length property smaller than its previous value, the array is truncated, and any elements with array indexes equal to or greater than the new value of the length property are lost.

If you make the length property larger than its previous value, the array is expanded, and any new elements created have the value undefined.

The following example illustrates the use of the length property:

var a;  
a = new Array(0,1,2,3,4);  
document.write(a.length);  
  
// Output  
// 5  
  

Requirements

[!INCLUDEjsv2]

Note

Starting with Internet Explorer 9 Standards mode, trailing commas included in the initialization of an Array are handled differently.