An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
August 19, 2022 02:28 pm GMT
Original Link: https://dev.to/shubhamtiwari909/js-push-and-pop-with-arrays-33a2
JS push and pop with Arrays
Hello Today i will be discussing about inbuilt push() and pop() method of Arrays.
Let's get started...
Push - It is used to insert an element at the end of the array.
Pop - It is used to remove the element from the array.
Code Example -
const array = [1,2,3,4,5];const array2 = ["This","is","array2"]array.push(6) //single element insertionarray.push(7,8,9) // multiple element insertionarray.push("BOOTSTRAP5") // string element insertionarray.push("TAILWINDCSS","REACT JS") //multiple string element insertionarray.push([10,11]) // number array insertionarray.push(["NODE JS","MONGO DB"]) // string array insertionarray.push([[12,13],[14,15]]) // 2-d array insertionarray.push({name:"shubham",age:21}) // Object insertionarray.push(array2) // array stored in a variable then inserted array.push(undefined,null) // undefined and null insertionarray.push(true,false) // Boolean insertionarray.push(array) // [Circular *1]console.log(array)array.pop() // pop out the last elementarray.pop() // pop out the last element
Output -
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 'BOOTSTRAP5', 'TAILWINDCSS', 'REACT JS', [ 10, 11 ], [ 'NODE JS', 'MONGO DB' ], [ [ 12, 13 ], [ 14, 15 ] ], { name: 'shubham', age: 21 }, [ 'This', 'is', 'array2' ], undefined, null, true, false, [Circular *1]]After popping 2 times[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 'BOOTSTRAP5', 'TAILWINDCSS', 'REACT JS', [ 10, 11 ], [ 'NODE JS', 'MONGO DB' ], [ [ 12, 13 ], [ 14, 15 ] ], { name: 'shubham', age: 21 }, [ 'This', 'is', 'array2' ], undefined, null, true]
- As you can see we can push many tyes of elements in the array.
- At the last push, we have pushed the array itself and it returned "[Circular *1] , Circular reference is a references where an object references itself directly or indirectly through an object.
- After using pop() two times, the last two elements are removed.
THANK YOU FOR CHECKING THIS POST
^^You can help me by some donation at the link below Thank you ^^
--> https://www.buymeacoffee.com/waaduheck <--
Also check these posts as well
https://dev.to/shubhamtiwari909/javascript-map-with-filter-2jgo
https://dev.to/shubhamtiwari909/e-quotes-3bng
https://dev.to/shubhamtiwari909/deploy-react-app-on-netlify-kl
Original Link: https://dev.to/shubhamtiwari909/js-push-and-pop-with-arrays-33a2
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To