JavaScript
  Home arrow JavaScript arrow Defining JavaScript Arrays Using Literal N...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Defining JavaScript Arrays Using Literal Notation
By: JavaScript Kit
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2003-09-22

    Table of Contents:
  • Defining JavaScript Arrays Using Literal Notation
  • Putting It to the Test

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Defining JavaScript Arrays Using Literal Notation


    (Page 1 of 2 )

    In this article, George introduces us to the process of creating arrays in JavaScript using literal notation. Is it really that simple? George attemps to show us how easy it really is.

    Literal notation is a form of array declaration introduced in JavaScript 1.2. Like the language version, it's supported by 4th+ generations of browsers.

    To declare a literal notation (array), start out with square brackets, then enclose each participating value inside, separated by commas:

    var myarray=["Joe", "Bob", "Ken"]

    Once declared, a literal notation behaves very much like a normal array, so to call to Joe, you would use:

    alert(myarray[0]) //Yo Joe what's up?

    At this point, we can all discern at least one merit of literal notation – its compact syntax. Literal notation puts even
    dense arrays to shame when it comes to quickly declaring an array and populating it with values.

    Ok, moving on, let’s explain the kind of values literal notation supports. Apart from the obvious "string" and "numeric" input, you can also use expressions:

    var myarray=[x+y, 2, Math.round(z)]

    If you can't make up your mind what to enter, undefined values are accepted too, by using a comma (,) in place of the value:

    var myarray=[0,,,,5]

    In the above, there are actually 6 array values, except the ones in the center are all undefined. This is useful, for example, if you wish to come back later to dynamically fill in these values, or set up your array for future expansion.

    More JavaScript Articles
    More By JavaScript Kit


     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek