Jump to content

Literal arrays VS. Condensed Arrays


w3schoon

Recommended Posts

Nice be with you everyone! While reading http://www.w3schools...s_obj_array.asp I've noticed two confusing arrays... 2: Condensed:

var myCars=new Array("Saab","Volvo","BMW");

3: Literal:

var myCars=["Saab","Volvo","BMW"];

My question is: What is the difference between Literal arrays & Condensed Arrays? & which of them is better to use? Thank you.

Edited by w3schoon
Link to comment
Share on other sites

a = [3]; // [3]a = new Array(3); // [ , , ]

They behave differently. You should only use an array literal unless you specifically want to create an array of a certain length (useful for making a string with 50 instances of a word, for example.) There are a few reasons for this; it is cleaner, the Array variable can be overwritten, no need for scope resolution which makes it more efficient, and no strange behaviours that you need to worry about.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...