Member-only story

JavaScript Array methods push() ,unshift() and splice()

Atiq ur rehman
1 min readSep 19, 2023

Pushing a items in array by different ways using javascript

Ways to push values into array using javascript
Differnet ways to push items into array using javascript

1.First Method push():

By using Push method we add item at the end of array:
e.g:

const countries = ["Nigeria", "Ghana"];

countries.push("Kenya");

console.log(countries);

//result

// ["Nigeria","Ghana","Kenya"]

2.Second Method unshift():

By using Unshift we add item at the start of array
e.g:

const countries = ["Nigeria", "Ghana"];

countries.unshift("South Africa");

console.log(countries);

//result

// ["South Africa", "Nigeria", "Ghana"]

3.Third Method splice():

Its hard but not as much hard you think you use your mind one time and you will grab its all behviour how that works

Array.splice(start_position, 0, new_element...);

start_position: means in which index you want to add your item

0 means you did not want to do with anything with previous array. You need to add my items to array.

Alert! Do not change that number 0 to any other

new_element: means these are your elements you want to add into array

Now lets check example Plz you must run it your own as well and play with as well

const countries = ["Nigeria", "Ghana"];

countries.splice(1,0,"us","uk")

console.log(countries);

//result

// ["Nigeria", "us", "uk", "Ghana"]

Thanks for reading stay connected

Javascript book i will recommend you:Book

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Atiq ur rehman
Atiq ur rehman

No responses yet

Write a response