Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Array Methods | Variables and Data Types
Introduction to JavaScript
course content

Conteúdo do Curso

Introduction to JavaScript

Introduction to JavaScript

1. Basic Concepts
2. Variables and Data Types
3. Basic Operations
4. Conditional Statements
5. Loops
6. Functions

bookArray Methods

Arrays are versatile for storing and retrieving data. Retrieving data using square brackets [] is called indexing.

However, there are various methods available for working with arrays.

Adding Elements

There are different ways to add elements to an array.

Push

The push() method adds a new value to the end of the array:

1234567
let arr = [1, 2, 3]; arr.push(4); arr.push(5); arr.push(6); console.log(arr);
copy

Unshift

The unshift() method works like the push() method, but it inserts the value at the beginning of the array.

123456
let arr = [1, 2, 3]; console.log("Array:", arr); arr.unshift(222); // Insert element at the start console.log("Array:", arr);
copy

Indexing

You can append a new value by indexing:

123456
let arr = [1, 2]; arr[2] = 3; arr[3] = 4; console.log(arr);
copy

Indexing allows you to assign a value to a specified index, reassign a previous value, and more:

12345
let arr = [1, 2, 3]; arr[0] = 4; console.log("Array:", arr);
copy

To create a new element in the array without mistakes, you can use the push(value) method or the arr[arr.length] = value expression:

12345678910111213
let myArray = []; myArray[myArray.length] = "indexing"; console.log("After first indexing:", myArray); myArray.push("pushing"); console.log("After first pushing:", myArray); myArray[myArray.length] = "indexing"; console.log("After second indexing:", myArray); myArray.push("pushing"); console.log("After second pushing:", myArray);
copy

Deleting Elements

Sometimes, you may need to delete elements from an array. This can be done in different ways.

Pop

The pop() method deletes the last element in an array and allows you to save it to another variable:

1234567
let arr = [11, 22, 33, 44]; console.log("Array:", arr); let x = arr.pop(); // Remove and save the last element in `arr` to variable `x` console.log("Popped element:", x); console.log("Array now:", arr);
copy

Shift

The shift() method works like pop(), but it removes the first element from an array:

123456789101112
let arr = [11, 22, 33, 44, 55, 66]; console.log("Array:", arr); let popped = arr.pop(); // Remove the last element console.log("Popped:", popped); console.log("Array:", arr); let shifted = arr.shift(); // Remove the first element console.log("Shifted:", shifted); console.log("Array:", arr);
copy
1. How can we add an element to the end of the array?
2. How can we remove the last element in the array?
How can we add an element to the end of the array?

How can we add an element to the end of the array?

Selecione algumas respostas corretas

How can we remove the last element in the array?

How can we remove the last element in the array?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 7
We're sorry to hear that something went wrong. What happened?
some-alt