Asked 7 years ago
16 Jan 2017
Views 1085
jaydeep

jaydeep posted

how to append new value to array in JavaScript ?

how to append new value to array in JavaScript ?

Rasi

Rasi
answered Nov 30 '-1 00:00

its very easy to append new value to array in JavaScript


var d=["i","am"];
d[1]="expert";
d.push("developer");
console.log(d);//["i", "expert", "developer"]

one can use .push function to push to array which will append at last of array
d.push("developer");
it will add developer in the list at end of array

and one can reset or add new by use of index of array
d[1]="expert";
it will add value expert to index -1 if not exist index or reset it with value expert.


noob

noob
answered Nov 30 '-1 00:00

you can use push function to append new value to array


var a = [];
a.push(1, 2, 3);
console.log(a);


you can use multiple argument to push to append multiple value to array in JavaScript .

Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

one can use spread syntax to append to array with push function

var a = [1];
var anew = [3,4,5];
 a.push(...anew)
console.log(a);//[1, 3, 4, 5]

if you try to push directly array to another array . it push array instead of its value and become multidimensional array


var a = [1];
var anew = [3,4,5];
 a.push(anew)
console.log(a);//[1, Array[3]]


so please note the difference between push array to array and push array 's value to another array .
Post Answer