Home arrow JavaScript arrow Defining JavaScript Arrays Using Literal Notation

Defining JavaScript Arrays Using Literal Notation

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.

Author Info:
By: JavaScript Kit
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
September 22, 2003
  1. · Defining JavaScript Arrays Using Literal Notation
  2. · Putting It to the Test

print this article

Defining JavaScript Arrays Using Literal Notation
(Page 1 of 2 )

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.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials