Introduction
In the real world, when we find standard features in a group of elements or a group of elements belonging to a specific category, we often store them together. In the future, when we discover more similar elements, we will add them to that group.
In computer science, we have such containers that can hold more than one element. Talking specifically about Javascript, we have arrays.
This article will discuss two of the Array methods in JavaScript, namely, push() and concat().
Check out the blog on the Fibonacci Series in Java, to know more about it.
Definition
push()
push() function in JavaScript, adds one or more values in the array. The additional values are indeed at the end of the array.
The syntax for push() is:
name_of_the array.push(value1, value2, value 3,...);
For example:
<script>
var example=[1,2,3,4]; //declaring the array
console.log(example); //prints [1,2,3,4]
example.push(5); //adding element to the array
console.log(example); //prints [1,2,3,4,5]
</script>
In the above program, initially, ‘example’ stores [1,2,3,4] (as the output on the console).
Then we push the value ‘5 ’, which gets added at the end and thus makes the array [1,2,3,4,5]. This can be verified by printing the array. Similarly, if we want to add more than one element to the array, that can also be done.
<script>
var example=[1,2,3,4]; //declaring the array
console.log(example); //prints [1,2,3,4]
example.push(5,6,7,8); //adding element to the array
console.log(example); //prints [1,2,3,4,5,6,7,8]
</script>
Here, we have added more than one element to the end.
One thing to note is that, with push(), we are actually making changes in the original array that we are using the function on. For example, in the code given above, we actually changed the array ‘example’ from [1,2,3,4] to [1,2,3,4,5,6,7,8].
concat()
The concat() in JavaScript is used to merge two or more independent arrays or sets of elements.
The syntax for same is:
array_1.concat(array_2,array_3,...);
For example:
<script>
var example_1=[1,2,3,4]; //declaring the array
var example_2=[5,6,7,8]; //declaring the array
var merged=example_1.concat(example_2); //merging using concat()
console.log(merged); //prints [1,2,3,4,5,6,7,8]
</script>
In the above example, we merged the arrays, example_1’ and ‘example_2’, and stored the concatenated array in the variable ‘merged’. The variable ‘merged’ contains the elements from both the arrays: elements of ‘example_2’ after elements of ‘example_1’.
In case where arrays are not stored in a variable, merging can still be achieved as follows:
<script>
var merged=[1,2].concat([3,4]); //merging using concat()
console.log(merged); //prints [1,2,3,4]
</script>
More than one array can also be merged in a similar fashion.
<script>
var merged=[1,2].concat([3,4],[5,6,7]); //merging using concat()
console.log(merged); //prints [1,2,3,4,5,6,7]
</script>
In concat(), there is no change in the already existing arrays. Instead, a new array is created to store the merged array.
So, one major difference between push() and concat() is that push() induces changes in the original array, whereas concat() makes no change in existing array values.
You can practice by yourself with the help of Online Javascript Compiler for better understanding.
To know about loop and while loop click here