Variable Type: Var (old)
- Declaration:
var variableName;
- Example:
function exampleFunction() { var x = 10; var x = 20; // Redeclaration is allowed within the same scope console.log(x);
// Outputs 20 }
- Always Global: Yes
- Redeclare (+): Allows redeclaration within the same scope.
- Reassign (+): Allows reassignment after declaration.
- Scope: Function
- Hoisted (+): Yes, hoisted to the top of the function scope.
- Bind this (+): Binds to the global object.
this.x
refers to the global scope. - Explanation: The
var
keyword is function-scoped and allows both redeclaration and reassignment within the same scope. It is hoisted to the top of the function, and its behavior is often associated with the global object.
Variable Type: let
- Declaration:
let variableName;
- Example:
if (true) { let y = 'Hello'; // let y = 'World'; // Error: Redeclaration not allowed y = 'World'; // Reassignment is allowed console.log(y)
; // Outputs 'World' }
- Always Global: No
- Redeclare (-): Does not allow redeclaration within the same scope.
- Reassign (+): Allows reassignment after declaration.
- Scope: Block
- Hoisted (-): No, not hoisted to the top of the block scope.
- Bind this (-): Does not bind to the
this
keyword.this.y
is undefined in this context. - Explanation: The
let
keyword is block-scoped, preventing redeclaration within the same block. It allows reassignment and is not hoisted to the top of the block scope.
Variable Type: const
- Declaration:
const variableName;
- Example:
const PI = 3.14; // const PI = 3.14159; // Error: Redeclaration not allowed // PI = 3.14159; // Error: Reassignment not allowed console.log(PI); // Outputs 3.14
- Always Global: No
- Redeclare (-): Does not allow redeclaration within the same scope.
- Reassign (-): Does not allow reassignment after declaration.
- Scope: Block
- Hoisted (-): No, not hoisted to the top of the block scope.
- Bind this (-): Does not bind to the
this
keyword.this.PI
is undefined in this context. - Explanation: The
const
keyword is block-scoped and does not allow redeclaration or reassignment after declaration. It is commonly used for constants.
Variable Type: global
- Declaration:
variableName = value;
- Example:
globalVar = true; globalVar = false; // Reassignment is allowed console.log(globalVar); // Outputs false
- Always Global: Yes
- Redeclare (-): Does not apply as it does not require explicit declaration.
- Reassign (+): Allows reassignment after declaration.
- Scope: Global
- Hoisted (-): No, global variables are not hoisted.
- Bind this (-): Does not bind to the
this
keyword.this.globalVar
is undefined. - Explanation: Variables declared without
var
,let
, orconst
keywords become global variables, accessible throughout the entire code. They allow reassignment but are not hoisted.
Variable Type: undefined
- Declaration:
let z;
- Example:
function exampleFunction() { let z; // let z; // Error: Redeclaration not allowed console.log(z); // Outputs undefined }
- Always Global: No
- Redeclare (-): Does not allow redeclaration within the same scope.
- Reassign (-): Does not apply as it is undefined by default.
- Scope: Block
- Hoisted (-): No, not hoisted to the top of the block scope.
- Bind this (-): Does not bind to the
this
keyword.this.z
is undefined. - Explanation: Variables declared with
let
(orconst
) are initialized withundefined
by default. Redeclaration is not allowed, and they are block-scoped.