-3

I would like to combine two or more variables into the same array index but not do anything to the values, just place them together in the same array index

So

 var myArray[];
 var one= 1;
 var two = 2;
 etc...
 myArray.push("one" + "two") 
 document.write(myArray[0];

Should output 12 or 1 2 but not add them together to show 3.

user3929948
  • 37
  • 1
  • 6

2 Answers2

1

Remove double quotes and for converting to string just add a '' between them. This kind of converting is more efficience than String()

var myArray = [];
var one = 1;
var two = 2;
myArray.push(one + '' + two)
document.write(myArray[0]);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Saeed
  • 5,413
  • 3
  • 26
  • 40
0

You can do this, using String to convert the numbers into strings and then + will perform string concatenation instead of numeric addition.

const myArray = [];
const one = 1;
const two = 2;

myArray.push(String(one) + String(two));
console.log(myArray);
Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54