7

I have been learning some javascript recently and found out that you can store different data types in an array like this:

var myArray = [12, 23.5, "hello", true]; 

I have some Java background and this is not possible in Java as you would have to declare the data type otherwise you would get an error (int myArray = blah blah blah)

So my question is, in what situations would you use this instead of an object for example. Examples would be great. Thanks.

2
  • 1
    One example would be an array of arguments, as given to .apply(). Commented Jun 10, 2015 at 15:41
  • 1
    It's all like ArrayList<Object> in Java. Commented Jun 10, 2015 at 15:42

5 Answers 5

5

This is the case in any language that is not strongly typed. Your array members can be of different primitive and they also can be objects. In most cases you wouldn't want to use this because there is no clear structure to your array. You would rather have something like:

var data = {
    prop: 12,
    otherProp: 24.5,
    stringProp: "hello",
    boolProp: true
};
Sign up to request clarification or add additional context in comments.

3 Comments

great point there "because there is no clear structure to your array."
Yeah great point. Instead of having multiple data types in an array, it would probably be more appropriate to just structure it as a plain object.
I haven't said it's not possible to do that in java, I was just saying that JS, which is not strongly typed and any other language language like it, like python for example, allow this.
5

Although it is possible to store different data types in array, it is considered a poor programming style, because an Array by definition is homogeneous data structure.

How would one handle in uniform way such array:

[ 2.5 , [ 1, 2 ], ["a", "b", "c"], {min: 2, max: 5}  ]

for

  • sorting
  • serialization
  • memory utilization
  • algorithms i.e. for maximum value

It does not seem natural to have different types in array, does it?

Comments

3

From what I know, there's not really a rule or set of specific situations where it would be considered best practice to store values of different types all in the same array in JavaScript. This is possible just because JavaScript is dynamically typed. I don't think there's any other reason besides that.

Is it best practice to do so? IMHO, I don't think so, really. I think the reason why Java (and I mentioned Java because you said you have some background there) restricts array data types (as do many languages) is partly because it's cleaner and separates concerns. I think restricting variables/other objects in JavaScript to one data type is usually a good idea regardless of the "capability" of dynamic typing.

Edit

Jonathan Lonowski pointed out that a good application is in Function.prototype.apply, which makes sense. That's because you can pass in an array of data, all of which may have different types. That's a good situation where it would make sense that JavaScript would have dynamic typing in objects like arrays, etc.

5 Comments

…and everywhere else when you use Array for tuples.
@Bergi, yeah fair enough. I just haven't seen many production use cases where tuples containing varying data types are a focal point, unless you're working with maybe algorithm development or something of the sort (node.js, perhaps).
CSV lines are a good day-to-day example. And [key,value]-tuples are used all over ES6.
@Bergi, makes sense. I don't use ES6, so I guess my answer can be limited primarily to ES5. When you say CSV, do you mean parsing CSV data on the client? In what types of situations would you be realistically doing that? Not being argumentative, just being curious because I don't see that very frequently, but I could be thinking in the wrong context.
OK, let's generalize to "tabular data of any kind". CSV parsing is probably not what most JS applications do, and most certainly not on the front end.
1

Actually, it is posible in Java, you have to declare:

Object[] prove = {1, "hi"};

In Javascript is the same, an array of objects.

Comments

-1

Javascript consider arrays as an object, ie:arrays are special type of objects in Javascript

But the difference is objects in javascript uses names to access elements

  1: var PM = {firstName:"Narendra", lastName:"Modi", age:70};

  2:  PM.firstName returns Narendra.

But arrays of javascript numbered indexes.

  1: var fruits = ["Banana", "Orange", "Apple", "Mango"];

  2: fruits[2] returns Apple.

by using collections we can resemble Javascript's array properties in JAVA too.that is by using List in java you can able to store different data types in an array ie:arraylist.

1 Comment

This does not answer OP's question. He asked in what situation you would use an array of different types over an object, not what the difference between arrays and objects is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.