0

Why in the following array

var mensajes = [('juan', 'pedro', 'hola como andas?'), ('jorge', 'matias', 'hola! todo bien?'), ('pedro', 'matias', 'buen dia matias!!')];

when i print mensajes

["hola como andas?", "hola! todo bien?", "buen dia matias!!"]

shouldn't it be an array with only 1 value holding the whole text? or does the () are actually doing something and how is only storing the last value within the ()? i dont understand this. thanks in advance

2 Answers 2

3

Since you have used parentheses in your array literal, JS interpreter treats the wrapped code as an expression and , becomes a Comma operator. The Comma operator evaluates it's operands from left to right and returns the last operand. That's why the last value of the operands in parentheses is set as the array value.

Sign up to request clarification or add additional context in comments.

3 Comments

which is really strange that the JS grammar would allow this useless production
@self Actually the operator has it's own use cases. It's that specific case which unawarely misuses the operator. Check this: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…
Using parens to generate an expression context is very useful when dealing with short circuit logical operations and ternary. Like !!condition && (a.push(b),a) means if condition is satisfied push b to array a and return array a. This is essential since otherwise a.push(b) would just return the new length of a.
1

The () are making the array only store the last part of what you want to store.

To your question, no. Your code isn't 1 array that holds all the text. If you want it to print out ALL the text, you'll have to remove all the () you have inside the array.

Like this: var mensajes = ['juan', 'pedro', 'hola como andas?', 'jorge', 'matias', 'hola! todo bien?', 'pedro', 'matias', 'buen dia matias!!'];

You can change it to this if you want a specific array to be printed but not all of it:

var mensajes = [['juan', 'pedro', 'hola como andas?'], ['jorge', 'matias', 'hola! todo bien?'], ['pedro', 'matias', 'buen dia matias!!']];

This holds arrays WITHIN the array so you can print a single whole array if you go like console.log(mensajes[0]), which would print out only [juan, pedro, hola coma andas]. You can't print all the text at once with console.log(mensajes) in this case.

If you want this, you first write the array name and then brackets for the index value of the array you want.

Hope this helps!

2 Comments

that was a very quick answer, i am trying to solve a programming question, and in order to know who send the message to who i need to get the first value from each message so now i know why it only storages the last string. im assuming i can't change the var mensajes so ima think in something else. thanks for the answer!
No problem. Good luck on your programming question! If you found one of the answers helpful, vote and choose the best answer so next time someone else knows which one is better.

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.