In this blog series tutorial, I will be covering some of the basic JavaScript programming concepts.
This is geared toward beginners and anyone looking to refresh their knowledge.
See the Previous Level Here
Level 8 will cover:
- Accessing Multi-Dimensional Arrays With Indexes
- Manipulating Arrays With Push()
- Manipulating Arrays with Pop()
- Manipulating Arrays With Shift()
- Manipulating Arrays With Unshift()
Accessing Multi-Dimensional Arrays With Indexes
Multi-dimensional arrays can be referred to as an array of arrays.
Each set of brackets is a level where the outermost set of brackets are the first level.
let diceArray = [ [18,5,1], [6,10,20], [2,7,15], ]; diceArray[1]; [6,10,20] diceArray[1][1]; 10
Manipulating Arrays With Push()
The push method adds items to the end of an array.
let diceRoll = ["Roll D6", 4,5,2,6]; diceRoll.push(1,5); console.log(diceRoll); ["Roll D6", 4,5,2,6,1,5]
Manipulating Arrays with Pop()
Pop removes the last item from an array.
let moonBeam = [8,5,10]; let firstEnemy = moonBeam.pop(); console.log(firstEnemy); 10 console.log(moonBeam); [8,5]
Manipulating Arrays with shift()
Shift removes an item from the beginning of an array.
let moonBeam = [8,5,10]; let firstEnemy = moonBeam.shift(); console.log(firstEnemy); 8 console.log(moonBeam); [5,10]
Manipulating Arrays With Unshift()
The unshift method adds items to the beginning of an array.
let inventory = ["cloak", "magic ring", "long sword"]; inventory.unshift("gold coins"); console.log(inventory); ["gold coins", "cloak", "magic ring", "long sword"]