6

I´m concatenate two different arrays into one,

let myarray = [...myarray1, ...myarray2];

If myarray2 is undefined how can I make this concatenation in the best way?

 if(myarray2){
         let myarray = [...myarray1, ...myarray2];
    }

This is too ugly INMHO. Could i not do something like:

let myarray = [...myarray1, ...?myarray2];

1 Answer 1

26

You may use this syntax

let myarray = [...(myarray1 ?? []), ...(myarray2 ?? [])];
Sign up to request clarification or add additional context in comments.

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.