0% found this document useful (0 votes)
11 views

Learn JavaScript - Arrays Cheatsheet - Codecademy

The document discusses key properties and methods of JavaScript arrays. It explains that the .length property indicates the number of elements in an array. Array elements are accessed by index starting from 0. The .push() method adds one or more elements to the end of an array, and .pop() removes the last element. JavaScript arrays are mutable, meaning their values can be changed even when declared with const. Arrays can hold items of any data type.

Uploaded by

bilal.a6t9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Learn JavaScript - Arrays Cheatsheet - Codecademy

The document discusses key properties and methods of JavaScript arrays. It explains that the .length property indicates the number of elements in an array. Array elements are accessed by index starting from 0. The .push() method adds one or more elements to the end of an array, and .pop() removes the last element. JavaScript arrays are mutable, meaning their values can be changed even when declared with const. Arrays can hold items of any data type.

Uploaded by

bilal.a6t9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1/18/24, 10:43 AM Learn JavaScript: Arrays Cheatsheet | Codecademy

Cheatsheets / Learn JavaScript

Arrays

Property .length

The .length property of a JavaScript array indicates the const numbers = [1, 2, 3, 4];
number of elements the array contains.

numbers.length // 4

Index

Array elements are arranged by index values, starting at // Accessing an array element
0 as the first element index. Elements can be accessed
const myArray = [100, 200, 300];
by their index using the array name, and the index
surrounded by square brackets.
console.log(myArray[0]); // 100
console.log(myArray[1]); // 200
console.log(myArray[2]); // 300

Method .push()

The .push() method of JavaScript arrays can be used to // Adding a single element:
add one or more elements to the end of an array.
const cart = ['apple', 'orange'];
.push() mutates the original array and returns the new
length of the array. cart.push('pear');

// Adding multiple elements:


const numbers = [1, 2];
numbers.push(3, 4, 5);

https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-arrays/cheatsheet 1/2
1/18/24, 10:43 AM Learn JavaScript: Arrays Cheatsheet | Codecademy

Method .pop()

The .pop() method removes the last element from an const ingredients = ['eggs', 'flour',
array and returns that element.
'chocolate'];

const poppedIngredient =
ingredients.pop(); // 'chocolate'
console.log(ingredients); // ['eggs',
'flour']

Mutable

JavaScript arrays are mutable, meaning that the values const names = ['Alice', 'Bob'];
they contain can be changed.
Even if they are declared using const , the contents can
be manipulated by reassigning internal values or using names.push('Carl');
methods like .push() and .pop() . // ['Alice', 'Bob', 'Carl']

Arrays

Arrays are lists of ordered, stored data. They can hold // An array containing numbers
items that are of any data type. Arrays are created by
const numberArray = [0, 1, 2, 3];
using square brackets, with individual elements separated
by commas.
// An array containing different data
types
const mixedArray = [1, 'chicken', false];

Print Share

https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-arrays/cheatsheet 2/2

You might also like