- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript push(): Add a New Element at the End of an Array
The JavaScript push() method is used to add a new element at the end of an array. For example:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> const cities = ["Tokyo", "Bangkok", "Dubai", "Berlin"]; cities.push("Frankfurt"); document.getElementById("xyz").innerHTML = cities; </script> </body> </html>
The push() function is used in this code to add an element to the end of the "cities" array.
cities.push("Frankfurt");
The string "Frankfurt" is appended to the end of the "cities" array, making it the fifth element.
The innerHTML property of an HTML element with ID "xyz" is set to the contents of the cities array after the new element is added.
document.getElementById("xyz").innerHTML = cities;
The HTML element with ID "xyz" is chosen in this case using the document.getElementById() function. The cities array value, which is a string with each of the cities array's elements separated by commas, is then set as the value of the innerHTML property.
JavaScript push() Syntax
The syntax of the push() method in JavaScript is:
array.push(elementOne, elementTwo, elementThree, ..., elementN)
A minimum of one element is required.
Advantages of the push() function in JavaScript
- JavaScript's push() function is a straightforward and user-friendly way to add elements to the end of an array.
- The original array is altered by the push() function, which adds new elements to the end of the array without changing the other elements.
- An array can grow dynamically thanks to the push() function because new elements can be added at any time.
- Any type of data can be used with the push() function, including objects, strings, and numbers.
Disadvantages of the push() function in JavaScript
- The push() function can only add elements to the array's end. If elements must be added at a specific index or at the start of an array, other functions such as splice() and unshift() must be used.
- Although the push() function is a convenient way to add elements to an array, it modifies the original array, which may result in unintended consequences if the original array must be preserved.
- push() can be slow when adding elements to very large arrays because the function must first find the end of the array before adding the new elements. It is often more efficient in such cases to create a new array with the additional elements and assign it to the original variable.
- Most modern browsers and JavaScript engines support the push() function, but some older JavaScript versions do not.
« Previous Tutorial Next Tutorial »