First I suggest changing the learning strategy. First you learn the concepts then you go to codes. We are seeing many people asking here using the same strategy as you and our observation is that it is not working, people are skating in learning.
Array
I start with the question explicitly asked in the question.
What is the array for, what is its usefulness?
What is actually the array?
Specifically that array works more like a array associative, also called in some dictionary situations, a hash table or map. You can see more on What is the difference between a map, a dictionary, an associative array and a hash table?.
And This is how Javascript assembles its objects. See also Objects are similar to arrays?.
Then in this case you have a list of elements with key and value. The key will serve to access one of the elements and get the corresponding value.
The other question is confusing I do not know answer and the other answer does not answer either, if it was important I should rewrite this part to be clearer.
Proper code
In such an example it makes little sense because all information is available and this particular code is a huge complication. It serves only to present the mechanism. It is good to make this clear.
Then through the variable languages
will be determined which is the element you are interested in. Note that if the variable comes a data that does not match any existing key will give error. This code is not a good example so also.
And it actually shows that this mechanism is quite wrong, a simple switch
would solve the case well.
Then we go into another misuse of the mechanism, which is to create an anonymous function for something that doesn’t need to be anonymous.
What I’m trying to say is that you’re using a complex example and you’re likely to catch programming addictions doing so.
Understanding a concept is good, learning how a mechanism works is very interesting, but many examples they put around don’t teach you where to use, how to use, don’t give you a context, and so they teach you wrong.
I’m going to tell you how this code can be better written, even if it’s different from what’s originally in the question because that’s how it should be done. The code of the question.
function program(languages) {
switch (languages) {
case 'Javascript':
case 'Typescript':
case 'Ecmascript': return 'Belongs to the js universe!';
default : return 'Does not belong to the js universe!!'
}
}
console.log(program('Javascript'));
console.log(program('Go'));
I put in the Github for future reference.
Maybe a if
it would work even better, but I don’t know if it would be changed. Exactly so I think it would work well even with a conditional operator:
function program(languages) {
return (languages === 'Javascript' || languages === 'Typescript' || languages === 'Ecmascript') ? 'Belongs to the js universe!' : 'Does not belong to the js universe!!';
}
console.log(program('Javascript'));
console.log(program('Go'));
I put in the Github for future reference.
In addition to simpler tends to be faster running, and a little more robust.
In the context used there is no reason to use something more complicated than this. Can there be contexts that something like this is useful? It can, but it would need to be fully demonstrated, put this way leads people to program in a confusing way.
Of course, everyone can choose the way they think best to do it, I just gave the simple one. For example, someone must have thought that this form is wrong for having negatively answered, it is typical of those who prefer confusing things.
Thanks for the explanation Maniero, I know about arrays, only had not understood its use there in that context declared soon after the function. Thanks!
– user194399
You explicitly asked about it:
Para que serve o array e qual sua utilidade?
. I introduced this and showed you that in this context this is not the right way to do this code and justified in the answer.– Maniero
In the first paragraph already won my vote of usefulness, you have to make it bold.
– Augusto Vasques