1. Scoping
The Scope is the region of the code where a certain variable can be accessed.
In JavaScript there are two types of scope:
- Block scope
- Global scope
1.1 Block Scope
If a variable is declared with const or let within a curly brace ({}), then it is said to be defined in the Block Scope.
- if..else
- function (){}
- switch
- for..of, etc.
Example :
1.2 Global Scope
- If a variable is declared outside all functions and curly braces ({}), then it is said to be defined in the Global Scope.
- When a variable declared with letorconstis accessed, Javascript searches for the variable in the block scopes first followed by global scopes.
2. Hoisting
2.1 Function Declarations
Hoisting is a JavaScript mechanism where function declarations are moved to the top of their scope before code execution.
2.2 Function Expressions
Function expressions in JavaScript are not hoisted.
2.3 Arrow Functions
Arrow Functions in JavaScript are not hoisted.
3. More Array Methods
The
3.1 Map()
- The map()method creates a new array with the results of calling a function for every array element.
- The map()method calls the provided function once for each element in an array, in order.
Syntax :
- Here the callback is a function that is called for every element of array.
- currentValue is the value of the current element and index is the array index of the current element. Here index and arr are optional arguments.
3.2 forEach()
The
Syntax :
Here index and arr are optional arguments.
3.3 filter()
The
filter()method creates a new array filled with all elements that pass the test (provided as a function).A new array with the elements that pass the test will be returned. If no elements pass the test, an empty array will be returned.
Syntax :
Here index and arr are optional arguments.
3.4 reduce()
The
Syntax :
Here
3.5 every()
The
Syntax :
Here index and arr are optional arguments.
3.6 some()
The
Syntax :
Here index and arr are optional arguments.
3.7 reverse()
The
Syntax :
3.8 flat()
The
Syntax :
4. Mutable & Immutable methods
Mutable methods will change the original array and Immutable methods won't change the original array.
Mutable methods | Immutable methods |
---|---|
shift() | map() |
unshift() | filter() |
push() | reduce() |
pop() | forEach() |
sort() | slice() |
reverse() | join() |
splice(), etc. | some(), etc. |
Try out Mutable and Immutable methods in the JavaScript Code Playground.