Variables
From W3Schools.
Declare with var, let, and const (or nothing)
- The
varkeyword was used in all JavaScript code from 1995 to 2015. - The
letandconstkeywords were added to JavaScript in 2015. - You cannot re-declare a variable declared with
letorconst. - You cannot re-assign a variable declared with
const. - Variables defined with
letmust be declared before use. - Variables defined with
lethave Block Scope.
let person = "John Doe", carName = "Volvo", price = 200;
let carName; // Will have undefined as value
- $ is a valid character in a variable name. Programmers often use it as an alias for the main function in a JavaScript library.
- "" is a valid character in a variable name. A convention among programmers is to prefix private/hidden variables with "".
Block Scope and variables
- Before ES6 (2015), JavaScript had only Global Scope and Function Scope.
- let and const provide Block Scope in JavaScript.
Variables declared inside a { } block cannot be accessed from outside the block: { let x = 2; } // x can NOT be used here //