push and pop not adding arrays in Javascript

Viewed 39

I know that shift and unshift is used to remove or add elements at the beginning of an array.Below is the simple Javascript Code.

var arx = ["Plumber",12,14,15,[18,"Try"]];
console.log("The array is :- ",arx);
arx.shift();    // SHIFT removes element from beginning
console.log("The array after shift operation is :- "+arx);
arx.unshift(11,"Try",["ABC",34,"GTH"],999);  ////////////   LINE 1
console.log("The array array unshift operation is :- "+arx);

Below is the output

enter image description here

I am not able to understand why after shift opeartion , my entire array is being displayed in series like that without any square bracket and all. Even the last element which is itself an array [18,'Try'] has been broken into two different elements. Even the unshift opeartion is adding every element one by one and is not adding array ["ABC",34,"GTH"] from line 1 in the form of array.Can anyone tell me what am I doing wrong ?

1 Answers

If you notice, in your console.logs you are adding "+" sign before arx. Try replacing it with a comma

var arx = ["Plumber",12,14,15,[18,"Try"]];
console.log("The array is :- ",arx);
arx.shift();    // SHIFT removes element from beginning
console.log("The array after shift operation is :- ",arx);
arx.unshift(11,"Try",["ABC",34,"GTH"],999);  ////////////   LINE 1
console.log("The array array unshift operation is :- ",arx);

Related