When working with JavaScript, it’s important to know how to declare and use variables. Different keywords (var
, let
, const
, global
, and undefined
) affect how variables behave in your code. Let’s explore each one.
1. var
- Declaration:
var variableName;
- Example:
- Always Global: Yes, if declared outside a function.
- Redeclare: Allows redeclaration within the same scope.
- Reassign: Allows reassignment after declaration.
- Scope: Function.
- Hoisted: Yes, moves 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, meaning it exists within a function. It can be redeclared and reassigned. Variables declared with var
are hoisted to the top of the function, and they also bind to the global object if declared outside any function.
2. let
- Declaration:
let variableName;
- Example:
- Always Global: No.
- Redeclare: Does not allow redeclaration within the same scope.
- Reassign: Allows reassignment.
- Scope: Block.
- Hoisted: No, not hoisted.
- Bind
this
: Does not bind tothis
.
Explanation: The let
keyword is block-scoped, meaning it only exists within the block where it is defined. It prevents redeclaration within the same block but allows reassignment. Unlike var
, let
is not hoisted, so you cannot use it before it is declared.
3. const
- Declaration:
const variableName;
- Example:
- Always Global: No.
- Redeclare: Does not allow redeclaration.
- Reassign: Does not allow reassignment.
- Scope: Block.
- Hoisted: No, not hoisted.
- Bind
this
: Does not bind tothis
.
Explanation: The const
keyword is also block-scoped and does not allow you to redeclare or reassign the variable. It is mostly used for constants, values that do not change.
4. global
- Declaration:
variableName = value;
- Example:
- Always Global: Yes.
- Redeclare: Does not apply.
- Reassign: Allows reassignment.
- Scope: Global.
- Hoisted: No.
- Bind
this
: Does not bind tothis
.
Explanation: If you declare a variable without using var
, let
, or const
, it becomes a global variable. These variables can be reassigned and are accessible throughout your code.
5. undefined
- Declaration:
let z;
- Example:
- Always Global: No.
- Redeclare: Does not allow redeclaration within the same scope.
- Reassign: Not applicable; it is undefined by default.
- Scope: Block.
- Hoisted: No.
- Bind
this
: Does not bind tothis
.
Explanation: Variables declared with let
or const
are initialized with undefined
by default. They are block-scoped, and redeclaration within the same scope is not allowed.
Feature | var | let | const | global | undefined |
---|---|---|---|---|---|
Declaration | var variableName; | let variableName; | const variableName; | variableName = value; | let z; |
Scope | Function | Block | Block | Global | Block |
Always Global | Yes, if declared outside a function | No | No | Yes | No |
Redeclaration | Allowed | Not allowed | Not allowed | Does not apply | Not allowed |
Reassignment | Allowed | Allowed | Not allowed | Allowed | Not applicable |
Hoisted | Yes | No | No | No | No |
Bind this | Binds to the global object (this.x ) | Does not bind | Does not bind | Does not bind (this.globalVar ) | Does not bind (this.z ) |
Example | var x = 10; var x = 20; | let y = 'Hello'; y = 'World'; | const PI = 3.14; | globalVar = true; globalVar = false; | let z; console.log(z); |