var name = 'Bryan'
let age = 29
const tattoos = true
var name = 'Bryan'
let age = 29
const tattoos = true
var is dead, don't use it
let age = 29
const tattoos = true
let age = 30
let age = 30
const tattoos = true
let age = 30 //number
const tattoos = true //boolean
const name = 'Bryan' //string
let biggestFear = null //null
let myGoal //undefined
const friends = ['Corey', 'Cam', 'Taz', 'Landy']
const favoriteNumbers = [3, 333, 1989]
const randomInfo = ['male', 'Holladay', true, 5, 29]
const randomInfo = ['Bryan', 'Holladay', true, 5, 29]
const me = {
name: 'Bryan',
city: 'Holladay',
tattoos: true,
tattooCount: 5,
age: 29
}
The me object makes much more sense than our randomInfo variable above
const me = {
name: 'Bryan',
city: 'Holladay',
tattoos: true,
tattooCount: 5,
age: 29
}
function expression
let returnFour = function(){
return 4
}
function declaration
function returnFour(){
return 4
}
Two different ways to do the same thing. You should stick with function declarations though
function returnFour(){
return 4
}
function returnBryan(){
return 'Bryan'
}
function returnFriends(){
return ['Taz', 'Corey', 'Cam', 'Landy']
}
function returnMe(){
return {
name: 'Bryan',
age: 28,
tattoos: true
}
}
We can return any number of things from functions
returnFour() //4
returnBryan() //'Bryan'
returnFriends() //['Taz', 'Corey', 'Cam', 'Landy']
returnMe() //{ name: 'Bryan', age: 28, tattoos: true}
But how do we actually get that data? We invoke the function
You can name your functions whatever you want but their name should make sense.
function sayHello(){
return 'Hello'
}
sayHello() // 'Hello'
function addNumbers(){
return 2 + 2
}
addNumbers() // 4
Functions should be as dynamic and reusable as possible. Hard coding data is generally frowned upon.
We can use things call parameters, which are just place holders for data we will add in later
function add(num1, num2){
return num1 + num2
}
Parameters can be called anything you want but you have to be consistent and it should make sense.
function add(pizza, cat){
return pizza + cat
}
DON'T DO THIS!
function add(num1, num2){
return num1 + num2
}
When we invoke the function, we use things called arguments which will take the place of the parameters
add(3, 6) // 9
function add(num1, num2){
return num1 + num2
}
Becomes when invoked
The order in which you use your arguments also matters
function myFaveNum(num1, num2){
return `myFaveNum is ${num2}`
}
myFaveNum(3,19) //'myFaveNum is 19
myfaveNum(3,1) //'myFaveNum is 1
myfaveNum(0,33) //'myFaveNum is 33
JavaScript follows a pattern called scope. What this means is what data is accessible and usable in different areas of your code
//Global Scope
const name = 'Bryan'
function sayName(){
return name
}
sayName() //'Bryan'
function secretNumber(){
let secNum = 3 // local scope
}
const myFaveNum = secNum // Error: secNum is not defined
function secretNumber(){
let secNum = 3 // local scope
return secNum
}
const myFaveNum = secretNumber()