Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 59

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 59
some-alt