1. understanding Scope:
The scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.
As per the above definition of Scope, So, the point is limiting the visibility of variables and not having everything available everywhere in your code.
the scope is defined in the main two ways,
- Global Scope
- Local Scope
consider above code greeting variable should be global scope, it can access inside the function,
In the above code for local scope,
What is Closure?
A closure is the combination of a function and the lexical environment within which that function was declared.
A closure is an inner function that has access to the outer (enclosing) function’s variables — scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.
Let see a closure example below:
In this code, We have an outer function User() which returns an inner function as displayName (),
The inner function will have access to the variables in the outer function scope, even after the outer function has returned.
Use cases :
Because our application could include many functions and global variables from different source files, it’s important to limit the number of global variables. If we have some initiation code that we don’t need to use again, we could use the IIFE pattern. As we will not reuse the code again, using IIFE, in this case, is better than using a function declaration or a function expression.
Variable Declaration: Here the variable is registered in its corresponding scope, the scope of a variable is simply “where the variable can be used.” We’ll be talking more about scope in the next lesson.
These are examples of variable declarations:
undefinedeven before the assignment.
8.Any of the complex data structures (Array and Objects)
The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search for never occurs.
6.Array slice() Method :
The slice() method returns the selected elements in an array, as a new array object.
The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.
Note: The original array will not be changed.
7.Array slice() Method :
The split() method is used to split a string into an array of substrings and return the new array.
If an empty string (“ “) is used as the separator, the string is split between each character.
The split() method does not change the original string.
The reduce () method reduces the array to a single value.
The reduce () method executes a provided function for each value of the array (from left to right).
The return value of the function is stored in an accumulator (result/total).
The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
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.
map() does not execute the function for array elements without values.
this method does not change the original array.