let friends = ['Landy','Lindsey','Taz']
let faveNums = [3,33,333]
let people = [ {name: 'Joe',position: 'instructor'},
{name: 'Steven',position: 'boss'},
{name: 'Bryan',position: 'instructor'}, ]
let randomInfo = [29, 'red', true]
Arrays are zero-indexed. What this means is that arrays start their count at 0 rather than 1
let faveNums = [3, 33, 333, 3333]
let firstFaveNum =
let fourthFaveNum =
let lastFaveNum =
faveNums[0]
faveNums[3]
faveNums[faveNums.length - 1]
//3
//3333
//3333
Arrays are one of the two most common ways to store data. If you ever have a list of data, it will be stored in an array
You often don't have control of where your data is stored in an array or your array is so big that there would be no way to know exactly which index the piece of data you want is at. That is where loops come in.
for(var i = 0; i < arr.length; i++){ //action to be performed }
for(var i = 0; i <= 10; i++){
//action to be performed
}
Looping over entire array
Looping over array set number of times
for(var i = 0; i < arr.length; i++){
//action to be performed
}
Where to start in the array
How long to loop for
What to do with i var after the loop
var friends = ['Taz', 'Landy', 'Corey', 'Cam']
function findFriend(friend, arr){ for(var i = 0; i < arr.length; i++) { if(arr[i] === friend){ return 'Found ' + friend } } return 'No friend found :(' }
var friends = ['Taz', 'Landy', 'Corey', 'Cam']
function findFriend(friend, arr){
for(var i = 0; i < arr.length; i++) {
if(arr[0] === friend){
return 'Found ' + friend
}
}
return 'No friend found :('
}
findFriend( 'Corey' ,friends)
'Taz'
friends.length
'Corey'
'Corey'
'Corey'
friends
var friends = ['Taz', 'Landy', 'Corey', 'Cam']
function findFriend(friend, arr){
for(var i = 0; i < arr.length; i++) {
if(arr[0] === friend){
return 'Found ' + friend
}
}
return 'No friend found :('
}
findFriend( 'Corey' ,friends)
'Landy'
friends.length
'Corey'
'Corey'
'Corey'
friends
var friends = ['Taz', 'Landy', 'Corey', 'Cam']
function findFriend(friend, arr){
for(var i = 0; i < arr.length; i++) {
if(arr[0] === friend){
return 'Found ' + friend
}
}
return 'No friend found :('
}
findFriend( 'Corey' ,friends)
'Corey'
friends.length
'Corey'
'Corey'
'Corey'
friends
Found Corey so we enter below and hit return statement
But what was that .length you used for your looping?
- You
Arrays have a variety of built in methods, or functions, on them. These come in handy so you don't have to figure out the logic yourself
.length
.push
.pop
.shift
.unshift
.slice
.splice
.forEach
.map
.filter
.reduce
.length
Finds the length of the array. One thing to keep in mind here is that it starts at 1
let friends = ['Landy', 'Lindsey', 'Taz']
friends.length
// 3
let nums = [1, 4, 0, 55, 666, 34, 954, 7]
nums.length
// 8
.push
Adds item to the end of the array. This piece of data can be anything. Alters length
let friends = ['Landy', 'Lindsey', 'Taz']
friends.push('Sperry')
friends
//['Landy', 'Lindsey', 'Taz', 'Sperry']
.pop
Removes the last item of an array and returns it. Alters the length of the array
let friends = ['Landy', 'Lindsey', 'Taz']
friends.pop()
friends
//['Landy', 'Lindsey']
.shift
Removes the first item of array and returns it. Alters length of the array
let friends = ['Landy', 'Lindsey', 'Taz']
friends.shift()
friends
//['Lindsey', 'Taz']
.unshift
ADDS item(s) to beginning of array and returns it. Alters length of the array
let friends = ['Landy', 'Lindsey', 'Taz']
friends.unshift('Joe', 'Eric')
friends
//['Joe','Eric','Landy','Lindsey', 'Taz']
.slice
Returns a copy of the selected portion of the array. Original array is not altered
let friends = ['Landy', 'Lindsey', 'Taz']
friends.slice(0, 1)
//['Landy']
friends.slice(2, 3)
//['Taz']
friends.slice(1, 3)
//['Lindsey', 'Taz']
friends.slice()
//['Landy','Lindsey', 'Taz']
.splice
Alters array by removing and possibly adding items to it. Returns removed item.
let friends = ['Landy', 'Lindsey', 'Taz']
friends.splice(0, 1, 'Fred')
//['Landy']
friends
//['Fred', 'Lindsey', 'Taz']
friends.splice(1, 2)
//['Lindsey', 'Taz']
friends
//['Fred']
let me = { name: 'Bryan', age: 29, tattoos: { has: true, count: 5 } }
let me = { name: 'Bryan', age: 29, tattoos: { has: true, count: 5 } }
me.name
// 'Bryan'
me.tattoos.count
// 5
me['age']
// 29
dot notation
bracket notation
let me = { name: 'Bryan', age: 29, tattoos: { has: true, count: 5 }, greet: function(){ return `Hello friends!` } }
me.greet
me.greet()
greet()
me['greet']()
function createEmployee(name, position, isAdmin){ return { name: name, position: position, admin: isAdmin } }
createEmployee('Bryan', 'instructor', false)
{name: 'Bryan', position: 'instructor', admin: false}
var people = [ {name: 'Bart', age: 33, friend: true},
{name: 'Phil', age: 46, friend: false},
{name: 'Lindsey', age: 24, friend: true}
]
condition ? value if true : value if false
A ternary is shorthand for if/else statements
var isAdmin = true
function checkAdmin(){ if(isAdmin === true){ return 'Access Granted' } else if(isAdmin !== true){ return 'Access Denied' } }
function checkAdmin(){ return isAdmin === true ? 'Access Granted' : 'Access Denied' }
// 'Access Granted'
But when should you use a ternary vs a if/else statement?
A good rule to follow is if you ever need to go deeper than checking just one condition. Nesting ternaries can be difficult to follow and often leads to a bit of confusion.
Ternary
If/Else
This OR that but nothing else
This OR this OR this OR this OR that
A callback, in its most simple form, is a function that will be referenced, or called back, to at a later time. Using callbacks makes your code more reusable
function personCreator(first, last){ var person = first + ' ' + last return person }
function personGreeter(callback, first, last){ var namedPerson = callback(first, last) return 'Hello, ' + namedPerson }
personGreeter(personCreator, 'Bryan', 'Smith')
function doMath(mathType, number1, number2){ return mathType(number1, number2) }
function add(number1, number2){ return number1 + number2 }
function subtract(number1, number2){ return number1 - number2 }
function multiply(number1, number2){ return number1 * number2 }