Home Login Registration Authors Contact Us About Us Subscribe
Want to receive new articles via e-mail? Click here!
: Home  :: Web Programming  :: JavaScript
Defining Javascript Arrays using Literal Notation
by Webdevinfo - Webmaster
Views: 425
Votes: none
Rating: 0.00
Synopsis:
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.
Rate this article  Print this article  Email this article  View this article in PDF  Discuss this article
Page:  1
The Article

 

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, lets 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.

Creating multi-dimensional arrays using literal notation.

The thing about defining multi-dimensional arrays using the standard syntax is that we're often the ones taken into the next dimension before the process is over. Or is it just me? At any rate, literal notation simplifies things for everyone, as it supports nesting of other literal notation. This makes defining multi-dimensional arrays extremely easy and intuitive. The following example uses literal notation to define a array with 3 elements, the 1st element being a 2 dimensional array:

var myarray=[["New York", "LA", "Seattle"], China, Japan]

Yes, it's that easy. By merely using another literal notation as one of the array values, we add a 2nd dimension to that particular element. Let's go to "LA" shall we:

myarray[0][1] //returns "LA"

Just to demonstrate literal notation's' versatility in this respect, here's an array with its 1st element in turn being a 3 dimensional array:

var myarray=[[[2,4,6]], China, Japan]

Just remember, the more brackets you use, the deeper the hole you dig! To conclude this tutorial, here are two example scripts that use literal notation to greatly compact the code otherwise required using conventional array:

1) JavaScript Graph It
2) NosTree


About the Author:
George is the webmaster of JavaScript Kit,
http://www.javascriptkit.com, a comprehensive site featuring tutorials and scripts on JavaScript, DHTML, CSS, and web design.
Page:  1
Rate this article  Print this article  Email this article  View this article in PDF  Discuss this article
Similar/related articles:
Advanced Search
Site Search:


FirstWebHosting
Top Ten Hosts as picked by our editors - with reviews and interviews.
The Host Planet
Web hosting reviews and ratings. Learn how to spot a great host.
Hosts2002
The first and greatest hosting directory with the consumer in mind.
Hostcue.com
Hosting directory for the masses with special offers Check us out!
WebDevForums
Web developers or all levels discuss the details of design and ecommerce.
Needscripts.com
Free scripts and applications for web developers.