Let’s say you want to buy some groceries from the market, and you wrote them down in a list.
In JavaScript, this “list” needs to be placed in an array. An array is a special kind of object that stores list-like information.
Creating arrays
You create an array by writing square brackets:
const emptyArray = []You can add values to the array as you create it. Make sure you separate each value with a comma.
const groceriesToBuy = ['cabbage', 'tomato sauce', 'salmon']Array values
Arrays can contain any valid JavaScript value. It can store primitives (like Strings and Numbers) and objects (like objects, other arrays, and even functions).
const strings = ['One', 'Two', 'Three', 'Four']const numbers = [1, 2, 3, 4]const booleans = [true, true, false, true]const objects = [{ name: 'Zell' }, { name: 'Vincy' }]const arrays = [ [1, 2, 3], [1, 2, 3],]Arrays can get difficult to read if everything is written in a single line. You can put each item on a new line to make it easier to read.
// One item in one lineconst groceriesToBuy = ['cabbage', 'tomato sauce', 'salmon']
// Arrays separated with new lines to make them easier to readconst arrays = [ [1, 2, 3], [1, 2, 3],]
// Objects separated with new lines to make them easier to readconst objects = [ { firstName: 'Zell', lastName: 'Liew' }, { firstName: 'Vincy', lastName: 'Zhang' },]Checking the number of items in an array
Arrays have properties and methods since they are objects. You can check the number of items in an array with the length property.
const numbers = [1, 2, 3, 4]console.log(numbers.length) // 4Getting the value of an item
To get the value of an item in an array, you write the name of the array, followed by the position of the item in square brackets. This position is called the index.
// Where n is the indexconst item = array[n]In JavaScript:
- First item has index 0,
- Second item has index 1
- Third item has index 2
- And so on.
Indexes that start from zero are called zero-based indexes.
const strings = ['One', 'Two', 'Three', 'Four']
const firstItem = strings[0] // Oneconst secondItem = strings[1] // Twoconst thirdItem = strings[2] // Threeconst fourthItem = strings[3] // FourIf you provide an index that exceeds the number of items in the array, you’ll get undefined.
const tenthItem = strings[9]console.log(tenthItem) // undefinedGetting items from the end
If you want to get the last item in the array, you can use Array.length to help you out:
const strings = ['One', 'Two', 'Three', 'Four']const lastItem = strings[strings.length - 1]
console.log(lastItem) // FourHere’s how this works:
- The last item,
Fouris in the fourth position. - Fourth position means index
3. array.lengthcounts the number of items in the array. (This gives4).- To get the last item, you can use
array.length - 1(which is4 - 1 = 3).
Feel free to use the array.length method to get the second last, third last, or even the fourth-last item.
const strings = ['One', 'Two', 'Three', 'Four']
const lastItem = strings[strings.length - 1] // Fourconst secondLastItem = strings[strings.length - 2] // Threeconst thirdLastItem = strings[strings.length - 3] // TwoSetting the value of an item
You can change the value of an item by assigning a value to it. You use = to assign a value.
const strings = ['One', 'Two', 'Three', 'Four']
// Assigning a new value to the first itemstrings[0] = 1
console.log(strings) // [1, 'Two', 'Three', 'Four']If you provide an index that exceeds the number of items in the array, JavaScript will help you create the intermediary items and fill them with undefined. (On Google Chrome, it may say empty. Nobody knows why they use empty).
For example, let’s say you have an array that contains four items. You add a string, ten as the tenth item.
const strings = ['One', 'Two', 'Three', 'Four']strings[9] = 'Ten'
console.log(strings)
Here, JavaScript creates items 5, 6, 7, 8 and 9 and fills them with undefined before setting the tenth item as Ten.
Exercise
- Make an empty array that contains nothing.
- Make an array that contains three items.
- Given this a list of
people(below), do the following:- Get
Franklin Rooseveltfrom thepeoplearray - Set
Thomas EdisontoInventor of the lightbulb
- Get
let people = ['Franklin Roosevelt', 'Thomas Edison', 'Benjamin Franklin']Make an empty array that contains nothing.
const emptyArray = []Make an array that contains three items.
const filledArray = ['one', 'two', 'three']Get Franklin Roosevelt from the people array
const franklink = people[0]Set Thomas Edison to Inventor of the lightbulb
people[1] = 'Inventor of the lightbuld'Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson