Skip to content

Instantly share code, notes, and snippets.

View djD-REK's full-sized avatar
🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞

Dr. Derek Austin djD-REK

🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞
View GitHub Profile
// let example
{ let variable = 3 } // Created in local scope (the current block scope)
console.log(variable) // ReferenceError: variable is not defined
// var example
{ var variable = 37 } // Created in global scope (the current function scope)
console.log(variable) // 37
// const example
const object = {hello: "world"} // Created in local scope (the current block scope)
for(let i = 0; i < 3; i++) { console.log(i) } // 0, 1, 2
console.log(i) // ReferenceError: i is not defined
var variable = 37
function myFunction () {
var variable = 40
console.log(variable) // 40
}
console.log(variable) // 37
myFunction()
console.log(variable) // 37
// Output: 37, 40, 37
var variable = 37
function myFunction () {
variable = 40
console.log(variable) // 40
}
console.log(variable) // 37
myFunction()
console.log(variable) // 40
// Output: 37, 40, 40
const constant = 37
console.log(constant) // 37
constant += 1 // TypeError: invalid assignment to const `constant'
function myFunction () {
console.log(variable) // 27
variable = 40 }
console.log(variable) // undefined
variable = 27
myFunction()
console.log(variable) // 40
var variable = 37
// Output: undefined, 27, 40
function myFunction () {
console.log(variable) // undefined
var variable = 40 }
console.log(variable) // undefined
variable = 27
myFunction()
console.log(variable) // 27
var variable = 37
// Output: undefined, undefined, 27
let variable = 37
function multiplyBySomeNumber(number) {
return function(innerNumber) {
return innerNumber * number
}
}
// equivalent to multiplyBySomeNumber = number => innerNumber => number * innerNumber
const multiplyByFive = multiplyBySomeNumber(5)
let variableTimesFive = multiplyByFive(variable)
console.log(variableTimesFive) // 185
// global var example
function myFunction() {
whoops = 3
}
console.log(whoops) // ReferenceError: whoops is not defined
myFunction()
console.log(whoops) // 3
// global var example
function myFunction() {
whoops = 3
}
console.log(whoops) // undefined
myFunction()
console.log(whoops) // 3
var whoops