4

If you have 2 arrays classical and pop:

classical=["Beethoven","Mozart","Tchaikovsky"];
pop=["Beatles","Corrs","Fleetwood Mac","Status Quo"];

Why is that when you set all=classical+pop it gives sets character in the array elements an individual character?

How do you correct this without retyping i.e. all=["Beethoven","Mozart","Tchaikovsky","Beatles"...]

Many thanks in advance.

2 Answers 2

7

Use the Array class concat() method to combine them on a new variable:

var all = classical.concat(pop);
Sign up to request clarification or add additional context in comments.

Comments

4

+ is first converting both arrays to string, and then adding the strings. For this you need to use concat method.

> classical=["Beethoven","Mozart","Tchaikovsky"];
["Beethoven", "Mozart", "Tchaikovsky"]
> pop=["Beatles","Corrs","Fleetwood Mac","Status Quo"];
["Beatles", "Corrs", "Fleetwood Mac", "Status Quo"]
> all = classical.concat(pop)
["Beethoven", "Mozart", "Tchaikovsky", "Beatles", "Corrs", "Fleetwood Mac", "Status Quo"]

Comments

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.