The length of any Javascript array can be found by using its length property. Moreover, the length property can be used to loop through array elements. Array numbering starts from 0 in Javascript. Therefore, name_of_the_array[1] is the second element of the array. As a result, the last element of the array is the total size of the array minus 1.

var myarray = ['a', 'b', 'c', 'd'];

Getting the first element of a Javascript Array:

document.write(myarray[0]);

This will display a as the first element of the array.

Getting the last element of a Javascript Array:

document.write(myarray[myarray.lenth-1]);

This will display d as the last element of the array.

Looping through the elements of a Javascript Array:

var myarray = ['a', 'b', 'c', 'd'];
for(i=0;i {
   document.write(myarray[i]);
}

This will display abcd.

Assigning a value to Javascript Array Length Property

If we assign a value to length property of the array which is less than the actual length of the array, the elements after the assigned length are lost.

Check the following example:

var myarray = ['a', 'b', 'c', 'd'];
myarray.length=2;
document.write(myarray[myarray.lenth-1]);

We lost the last two elements by assigning a value smaller than the actual length of the array. Therefore, the example above will display b.

Array Length Browser Incompatiblity

The following code returns 4 in Firefox and 5 in IE.

var myarray = ['a', 'b', 'c', 'd',];
document.write(foo.length);

The extra comma on the end of the array causes the interpretation difference. IE interprets this as an additional element with an undefined value. Firefox ignores it.

The ECMAScript standard says:

Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined.

According to the standard the interpretation of IE is correct. However, you have to be careful to write your code compatible to all browsers.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 4.3 by 6 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories : All | JavaScript




Comments

April 21. 2008 18:39

Actually if you read the ECMAScript standard carefully IE is _wrong_. Only commas at the beginning without an AssignmentExpression preceeding, or multiple commas without AssignmentExpressions between them are considered array values (albeit "not defined"). Commas at the end without a trailing AssignmentExpression are not to be considered.

Otherwise except for misspelling "length" and the browser parser swallowing up your < in the for loop, nice and simple explanation.

Chris Craig

Add comment





(will not be displayed)








  • javascript
  • querystring
  • url parameters
  • parse querystring
  • delayed redirect
  • settimeout
  • focus
  • textbox
  • page load
  • after submit
  • set focus in asp.net 2.0/3.5
  • on page load
  • after postback
  • set control focus
  • postback
  • asp.net 1.x
  • substring
  • substr
  • javascript string methods
  • parsefloat
  • convert strings to numbers
  • parseint
  • javascript history
  • history.go
  • history.back
  • http requests
  • image maps
  • css sprites
  • external css
  • external javascript
  • compress javascript
  • javascript compression
  • ajaxcontroltoolkit
  • tab control
  • array
  • length
  • javascipt
  • lastmodified
  • mstsc
  • terminal services
  • remote desktop connections
  • null
  • undefined
  • array.join
  • string concatenation
  • setinterval
  • clearinterval
  • timing events
  • cleartimeout
  • javascript timing events
  • url redirection
  • location.href
  • location.replace
  • redirect
  • redirection
  • system.io.compression
  • viewstate compression
  • compress viewstate
  • gzipstream
  • loadpagestatefrompersistencemedium
  • savepagestatetopersistencemedium
  • form spam
  • captcha
  • prevent spam without captcha
  • url redirect
  • defaultbutton
  • enter key
  • default button
  • asp.net
  • 2.0
  • form
  • panel
  • 1.1
  • form submit
  • dopostback
  • onkeypress
  • onkeydown
  • onkeyup
  • javascript key events
  • keycode 13
  • disable enter key
  • int32.parse
  • convert.toint32
  • int32.tryparse
  • google
  • hoax
  • gmail
  • storage
  • counter
  • mail
  • visual studio 2005
  • vs 2008
  • copy
  • paste
  • clipboard data
  • static variables
  • application object
  • static property
  • server control
  • web file manager
  • iz web file manager
  • convert
  • parse
  • tryparse
  • file upload control
  • maxrequestlength
  • executiontimeout
  • httpruntime
  • asp.net 2.0
  • registering scripts
  • registerclientscript
  • registerstartupscript
  • cross-browser
  • events
  • improve web site performance
  • compression
  • caching
  • elmah
  • error logging
  • exception
  • error
  • httphandler
  • google sitemap generator
  • sitemap
  • internet information services
  • iis7
  • hosts file
  • localhost
  • windows vista
  • search engine optimization
  • seo
  • search engine friendly pages
  • headscriptmanager
  • class library
  • head
  • css
  • c#
  • iis
  • internet information services manager
  • 401.3 unauthorized
  • 500.0 internal server error
  • http error 500.19
  • google toolbar
  • yellow input fields
  • input
  • select
  • background color
  • mozilla firefox
  • medium trust
  • orcas
  • compileroptions
  • warninglevel
  • zyb
  • mobile phones
  • online backup service
  • online services
  • textarea
  • maxlength
  • limit input length
  • custom server control
  • internal error 2739
  • adobe cs3
  • adobe customer support
  • solution
  • error code 0x80004005
  • 500.19 internal server error
  • meta tags
  • keywords meta tag
  • meta
  • description meta tag
  • fake page rank domains
  • scammers
  • google page rank technology
  • ebay
  • general
  • title
  • page rank