Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Adding Values to an Array | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Essentials for Beginners - 1768407374405

bookAdding 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:

arrayName.push(value1, value2, …);

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 59

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookAdding Values to an Array

Scorri per mostrare il menu

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:

arrayName.push(value1, value2, …);

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 59
some-alt