add variable to array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

add variable to array

how to add multiple variables in an array in javascript? ex - var a = abc; var b = xyz ; var c = pqr; var arrdata = new array();

9th Feb 2017, 8:43 AM
sunny saini
sunny saini - avatar
3 Answers
+ 12
var arr = []; arr.push(x); ……………………
9th Feb 2017, 9:05 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
or arrdata = [ a, b, c, ... , z... ]
9th Feb 2017, 10:20 AM
WittyBit
WittyBit - avatar
+ 1
There are a number of ways of adding objects to an array. var array = [0, 2, 4]; // Array.prototype.push: Adds to the end of an array. Can take multiple arguments. array.push(6); // returns 4 (the new array length) // Array.prototype.unshift: Adds to the beginning of the array. Can take multiple arguments. array.unshift(8); // return 5 (the new array length) // Array.prototype.splice: // (first argument is the index to add the object, the second is the amount of // objects to remove starting from the index, and the third is the object to add at the index. array.splice(5, 0, 10); // returns an array of the removed elements The last one I have to write is what you can also use to set the array elements. It is in some cases faster than `array.push` (for me, it has always beaten `array.push`). It simply adds an object to the tail of the array. array[array.length] = 11;
9th Feb 2017, 11:43 AM
John
John - avatar