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']
-
2Tried 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.user924016– user9240162014年02月17日 05:19:53 +00:00Commented Feb 17, 2014 at 5:19
-
Or another approach: initialize the variables first into the "new" array variable.Claudia– Claudia2014年02月17日 05:22:52 +00:00Commented Feb 17, 2014 at 5:22
-
Maybe you where looking to var words = new Array(word1, word2)user924016– user9240162014年02月17日 05:24:59 +00:00Commented 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.adigioia– adigioia2014年02月17日 05:32:41 +00:00Commented Feb 17, 2014 at 5:32
1 Answer 1
you can use .push(), do:
var word1 = 'hello';
var word2 = 'good';
var arr = [];
arr.push(word1, word2);
console.log(arr); //["hello", "good"]
answered Feb 17, 2014 at 5:20
Sudhir Bastakoti
100k15 gold badges163 silver badges169 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js