Variables to Store Data

Var

  • var is function scoped- can only be accessible within the function, you can also redeclare variables
  • generally, don't use var- if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined, can lead to bugs
var x = 16;
var y = 4;  // variables are x y z and are declared with var keyword 
var z = x + y;
console.log(z)
20

Const

  • const values cannot be changed
  • general rule, always declare variables with const unless you think the variable will change
  • block-scoped so we can access const only within the block where it was declared.
const pi = 3.14;
console.log(pi)
3.14

let

  • use if you think variable will change
  • block scoped
let dog_name = "Fluffy";
console.log(dog_name)
Fluffy