Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 6, Topic 7
In Progress

JavaScript: .push() Method

Yasin Cakal 16 Nov 2021
Lesson Progress
0% Complete

Let’s look at some built-in JavaScript methods for working with arrays. These methods are specifically called on arrays to simplify common tasks such as adding and removing elements.

.push(), allows us to add items to the end of an array. Here’s an example of how this is put to use:

const evenNumbers = [2, 4, 6];
evenNumbers.push(8,10);
console.log(evenNumbers); 

// Output: [ 2, 4, 6, 8, 10 ]

So, how exactly does .push() work?

  1. We use dot notation to access the push method, connecting push to evenNumbers with a period.
  2. Then we refer to it as a function. This is due to the fact that .push() is a function that JavaScript allows us to use directly on an array.
  3. .push() accepts a single argument as well as multiple arguments separated by commas. In this case, we’re adding two elements to evenNumbers: 8 and 10.
  4. evenNumbers is changed or mutated by .push(). Because it modifies the initial array, .push() is also known as a destructive array method.

.push() is the method to use! If you’re looking for a method to mutate an array by adding elements to it.

Responses

Leave a Reply

%d bloggers like this: