In what situation will different types of data be stored in an array that will be useful in Javascript?

I recently studied javascript and found out that you can store different types of data in an array like this:

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

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

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

+6
source share
5 answers

This applies to any language that is not strongly typed. Your array members can be of different primitives, and can also be objects. In most cases, you will not want to use this because there is no clear structure for your array. Would you prefer something like:

 var data = { prop: 12, otherProp: 24.5, stringProp: "hello", boolProp: true }; 
+5
source

Although you can store different types of data in an array, it is considered a bad programming style, because by definition Array is a homogeneous data structure .

How can one process such an array in the same way:

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

for

  • Sorting
  • serialization
  • Memory usage
  • those. for maximum value

It seems natural to have different types in an array, right?

+4
source

From what I know, there really is no rule or set of specific situations in which it would be best to store values โ€‹โ€‹of different types in the same array in JavaScript. This is only possible because JavaScript is dynamically typed. I do not think there is another reason.

Is it better to do this? IMHO, I do not think so. I think the reason Java (and I mentioned Java because you said you had some kind of background there) limits the data types of the array (like many languages), partly because it cleans and separates the problems . I think that restricting variables / other objects in JavaScript to one data type is usually a good idea, regardless of the "ability" of dynamic input.

Edit

Jonathan Lonowski noted that a good application is in Function.prototype.apply , which makes sense. This is because you can pass an array of data, all of which can have different types. This is a good situation when it would be reasonable that JavaScript will have dynamic typing in objects such as arrays, etc.

+3
source

In fact, this is possible in Java, you should declare:

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

In Javascript is the same, an array of objects.

+2
source

Javascript treats arrays as an object, that is: arrays are a special type of object in Javascript

But the difference is that objects in JavaScript use names to access elements

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

But javascript arrays number the indices.

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

using collections, we can resemble the properties of a Javascript array in JAVA. That is, using List in java, you can store various data types in an array, for example: arraylist.

0
source

Source: https://habr.com/ru/post/988858/


All Articles