0

I know This is a simple question, but I'm new and I can't figure it out. I am trying to combine two variables into one array. I want to combine var word1 and var word2 to make var new. What is the proper way to do this?

var word1 = 'hello'
var word2 = 'good'

var newArray = ['hello','good']
4
  • 2
    Tried replacing the strings with the vars? [word1, word2]. Btw watch out with new keyword it is reserved. And is the probably what has been confusing you. Commented Feb 17, 2014 at 5:19
  • Or another approach: initialize the variables first into the "new" array variable. Commented Feb 17, 2014 at 5:22
  • Maybe you where looking to var words = new Array(word1, word2) Commented Feb 17, 2014 at 5:24
  • Yeah, I didn't realize I couldn't use new, but I was using different names in my script anyways. I just typed new in the question to make it simpler. Thanks for the help everyone, I knew it was simple, I am just starting out learning to code. Commented Feb 17, 2014 at 5:32

1 Answer 1

3

you can use .push(), do:

var word1 = 'hello';
var word2 = 'good';
var arr = [];
arr.push(word1, word2);
console.log(arr); //["hello", "good"]
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.