Var me = [] VS var me = new Object[]


#1

What is the difference between these two? What pro's and con's lies inside these examples?


var me = [];
me.name = "Simen";
me.age = 19;

VS

var me = new Object();
me.name = "Simen";
me.age = 19;


#2

objects and associative arrays can be used interchangeable, see this article

this:

var me = new Object();
me["name"]= "Simen";
me["age"] = 19;

is also perfectly valid


#3

Both are valid declarations, although if you would like to follow JS code conventions, it is advised to use [] instead of new Array()

Here is an excerpt:

{} and []

Use {} instead of new Object(). Use [] instead of new Array().

Use arrays when the member names would be sequential integers. Use objects when the member names are arbitrary strings or names.


#4

he uses []? So this is confirmation he follows the convetions?


#5

Yeah, looks like it :slight_smile:


#6

looking at the code conventions:

Use {} instead of new Object(). Use [] instead of new Array().

wouldn't it make more sense to say:

its advised to use {} instead of new Object(), given xelvir doesn't follow this coding convention


#7

The codeacademy Javascript tutorial (10 hours) did not say anything about using {} instead. It only said that I could use new Object to create a new Object().

So {} is the same as new Object(), just simplified?

Doesn't {} and [] act the same way?
Am I correct that Arrays are Objects and Objects Arrays?


#8

Well that's another thing - to determine what's more appropriate for the current situation? ..

Use arrays when the member names would be sequential integers. Use objects when the member names are arbitrary strings or names.


#9

see powell_v2 coding convention (this one),

no. arrays:

myArray = [1,2,3,4,5]

arrays are not the same as objects. associative and objects are the same (an can be used interchangeable)


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.