1. this
The
this
is determined in three ways.- In Object methods, it refers to the object that is executing the current function.
- In Regular functions, it refers to the windowobject.
- In Arrow functions, it refers to the context in which the code is defined.
1.1 this in Object Methods
JAVASCRIPT
In the above example,
this
refers to the car
object as it's executing the method start
.1.2 this in Regular Functions
JAVASCRIPT
In the above example,
this
refers to the window
object.1.3 this in Arrow Functions
In Arrow functions,
this
depends on two aspects:- When the code is defined
- Context
Arrow function inherits
this
from the context in which the code is defined.1.3.1 Object Methods
JAVASCRIPT
Arrow Functions with Callbacks
JAVASCRIPT
1.4 this in Constructor Functions
In Constructor Function,
this
refers to the instance object.JAVASCRIPT
In the above example,
this
refers to the object car1
.1.4.1 Arrow Functions
JAVASCRIPT
Try out the
this
in different functions like Object Methods, Arrow Functions, and Constructor Functions etc. in the JavaScript Code Playground.2. Immutable and Mutable Values
2.1 Immutable
All the primitive type values are immutable.
- Number
- String
- Boolean
- Symbol
- Undefined, etc.
JAVASCRIPT
2.2 Mutable
All the Objects are mutable.
- Object
- Array
- Function
JAVASCRIPT
3. Declaring Variables
In JavaScript, a variable can be declared in 3 ways.
- Using let
- Using const
- Using var
3.1 let
While declaring variables using
let
,- Initialization is not mandatory
- Variables can be reassigned
JAVASCRIPT
3.2 const
While declaring variables using
const
,- Initialization is mandatory
- Once a value is initialized, then it can't be reassigned
Without Initialization:
JAVASCRIPT
Reassignment:
JAVASCRIPT
3.2.1 Mutating Object properties
JAVASCRIPT
But objects can't be reassigned.
JAVASCRIPT
Try out the Mutable and Immutable values and declare the variables using
const
in the JavaScript Code Playground.