Naming
let userName = 'John', age = 25, message = 'Hello', test123 = true;
const myBirthday = '18.04.1982';
const COLOR_RED = "#F00";
Declaration (let, const, var) , let & const have block scope var has functional or global scoped, visible through blocks var variables can be declared below their use var tolerates re-declarations functions have block scope (in "use strict" at least) if , for , while are blocks
{
{
let var1 = "var1" // only visible in this block
const var2 = "var2" // only visible in this block
var var3 = "var3" // visible outside
console.log(var1) // "var1"
console.log(var2) // "var2"
console.log(var3) // "var3"
}
console.log(var1) // not defined
console.log(var2) // not defined
console.log(var3) // "var3"
}
{
if (true) {
let phrase = "Hello!";
alert(phrase); // Hello!
}
alert(phrase); // Error, no such variable!
}
{
function fn1() {
var x = 1
const y = 2
let z = 3
console.log(r, v, p) // not defined
fn2() // works
function fn2() {
console.log(x, y, z) // 1 2 3
var r = 666
const v = 666
let p = 666
}
}
console.log(x, y, z) // not defined
fn1() // works
fn2() // not defined
}
if (true) {
var test = true;
}
test // true
if (true) {
let test = true; // use "let"
}
test // ReferenceError: test is not defined