Variables
General
Info:
Variables are containers for storing values.
A JavaScript variable can hold any type of data.
usage
Always declare variables at the beginning of a script
Always use
const
if the value should not be changedAlways use
const
if the type should not be changed (Arrays and Objects)Only use
let
if you can't use constOnly use
var
if you MUST support old browsers.
Any variable can be emptied, by setting the value to undefined. The type will also be undefined.
car = undefined; // Value is undefined, type is undefined
Identifiers
Info:
- use camel case for identifiers
- use descriptive names like
totalSum
- Names can begin with a letter,
$
or_
Danger:
- Identifiers are used to name variables and keywords, and functions.
- JavaScript is Case Sensitive
- lastName and lastname are two different identifiers
- Names must begin with a letter.
_
commonly used for private / hidden variables
- Reserved words cannot be used as names.
let
Info:
let
has Block Scope- Redeclaring in another block IS allowed
Danger:
- must be Declared before use
- cannot be redeclared in same block
var
Info:
var
has global Scope, it is hoisted and can be used everywhere- can be redeclared
Danger:
- (re)declaration inside a { } block
- can be accessed from outside the block
- changes value globally
const
Info:
const
has Block Scope- Redeclaring in another block IS allowed
Danger:
- must be Declared before use
- cannot be redeclared in same block