Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Adding Values to an Array | Exploring Arrays
Introduction to JavaScript
course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
Adding Values to an Array

After defining an array, we can further add some elements to it using the push method.

Note
Definition

A methods is a function that belongs to an object and is called using that object. The main difference is that a function is independent, while a method is tied to an object and is called using object.methodName(). For example, in this case the object is the array and the method is push.

The general syntax of the push method is:

python

Here arrayName is the name of the array to which we want to add the element, and value1, value2 and so on are the elements we want to add to that array.

Pushing an element to an array adds it to the end of the array:

12345
let fruits = ["Apple", "Banana"]; console.log(fruits); // Output: ["Apple", "Banana"] fruits.push("Cherry"); console.log(fruits); // Output: ["Apple", "Banana", "Cherry"]
copy

Notice how "Cherry" was added to the end of the fruits array using the push method.

If we have multiple values to push, they are pushed in the same order they were mentioned:

12345
let fruits = ["Apple", "Banana"]; console.log(fruits); // Output: ["Apple", "Banana"] fruits.push("Cherry", "Mango"); console.log(fruits); // Output: ["Apple", "Banana", "Cherry", "Mango"]
copy

1. What does the push method do in JavaScript?

2. Which of the following is the correct syntax for using the push method?

3. What will be the output of the following code?

question mark

What does the push method do in JavaScript?

Select the correct answer

question mark

Which of the following is the correct syntax for using the push method?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 4

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
Adding Values to an Array

After defining an array, we can further add some elements to it using the push method.

Note
Definition

A methods is a function that belongs to an object and is called using that object. The main difference is that a function is independent, while a method is tied to an object and is called using object.methodName(). For example, in this case the object is the array and the method is push.

The general syntax of the push method is:

python

Here arrayName is the name of the array to which we want to add the element, and value1, value2 and so on are the elements we want to add to that array.

Pushing an element to an array adds it to the end of the array:

12345
let fruits = ["Apple", "Banana"]; console.log(fruits); // Output: ["Apple", "Banana"] fruits.push("Cherry"); console.log(fruits); // Output: ["Apple", "Banana", "Cherry"]
copy

Notice how "Cherry" was added to the end of the fruits array using the push method.

If we have multiple values to push, they are pushed in the same order they were mentioned:

12345
let fruits = ["Apple", "Banana"]; console.log(fruits); // Output: ["Apple", "Banana"] fruits.push("Cherry", "Mango"); console.log(fruits); // Output: ["Apple", "Banana", "Cherry", "Mango"]
copy

1. What does the push method do in JavaScript?

2. Which of the following is the correct syntax for using the push method?

3. What will be the output of the following code?

question mark

What does the push method do in JavaScript?

Select the correct answer

question mark

Which of the following is the correct syntax for using the push method?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 4
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt