Software-Engineering


# Which Variables exist in JS?

For variables, we use let, var, const

typescopedefinition
letblock scopecan be reassigned
constblock scopecan’t be reassigned
varfunction scope - old; avoid ithoised to the top of their scope
let firstName = 'Asabeneh'
firstName = 'Eyob'
 
const PI = 3.14 // Not allowed to reassign PI to a new value
// PI = 3.

# Block Scoped

use let, const

function letsLearnScope() {
  // you can use let or const, but gravity is constant I prefer to use const
  const gravity = 9.81
  console.log(gravity)
}
// console.log(gravity), Uncaught ReferenceError: gravity is not defined
 
if (true) {
  const gravity = 9.81
  console.log(gravity) // 9.81
}
// console.log(gravity), Uncaught ReferenceError: gravity is not defined
 
for (let i = 0; i < 3; i++) {
  console.log(i) // 1, 2, 3
}
// console.log(i), Uncaught ReferenceError: gravity is not defined

# Scoped to function

use var TRY TO AVOID var

function letsLearnScope() {
  var gravity = 9.81
  console.log(gravity)
}
// console.log(gravity), Uncaught ReferenceError: gravity is not defined
 
if (true) {
  var gravity = 9.81
  console.log(gravity) // 9.81
}
console.log(gravity) // 9.81
 
for (var i = 0; i < 3; i++) {
  console.log(i) // 1, 2, 3
}
console.log(i)