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

Learn JavaScript - Arrays Cheatsheet - Codecademy

Arrays in JavaScript are mutable lists that can hold elements of any data type. Arrays use square brackets and commas to define individual elements. Each element has a numeric index starting from 0. Elements can be accessed by their index. The .length property returns the number of elements in an array. The .push() method adds elements to the end of an array, and .pop() removes the last element. Though arrays can be declared with const, their contents are still mutable through methods like .push() and reassigning values.

Uploaded by

hevin nichola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Learn JavaScript - Arrays Cheatsheet - Codecademy

Arrays in JavaScript are mutable lists that can hold elements of any data type. Arrays use square brackets and commas to define individual elements. Each element has a numeric index starting from 0. Elements can be accessed by their index. The .length property returns the number of elements in an array. The .push() method adds elements to the end of an array, and .pop() removes the last element. Though arrays can be declared with const, their contents are still mutable through methods like .push() and reassigning values.

Uploaded by

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

Cheatsheets / Learn JavaScript

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

// An array containing different data


types
const mixedArray = [1, 'chicken', false];

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

console.log(myArray[0]); // 100
console.log(myArray[1]); // 200
console.log(myArray[2]); // 300

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

numbers.length // 4

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

// Adding multiple elements:


const numbers = [1, 2];
numbers.push(3, 4, 5);
Method .pop()
The .pop() method removes the last element from an
array and returns that element. const ingredients = ['eggs', 'flour',
'chocolate'];

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

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

You might also like