10 JavaScript concepts that every JavaScript Programmer must know.

This post covers some basic concepts in JavaScript. Every JavaScript programmer must know and master these.

1. understanding Scope:

A simple definition for a scope in JavaScript:

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,

In scope level variables in JavaScript ES6 has updated hoisting variable let, var, const type check with that, In order to learn the scope, you need to understand hoisting also.

2. JavaScript Closures:

What is Closure?

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.

3. IIFE

What is an IIFE in JavaScript?

IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

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.

4. Variables:

Variable are like containers in JavaScript. Using variables to store data is the foundation of the language, and it is done in three distinct parts.

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:

  1. Variable Initialization: This usually occurs when a variable is declared. Here the variable is assigned a memory or space by the JavaScript engine. Because of this, once a variable is declared, it takes a value of undefined even before the assignment.
  2. Variable Assignment: Variable assignment is usually the most important step when using a variable. Here the variable is assigned data which is a value using the assignment operator “=”. Values in JavaScript take one of the standard JavaScript datatypes which are:
  3. String

4.Number

5.Boolean

6.Null

7.Undefined

8.Any of the complex data structures (Array and Objects)

5. IndexOf():

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.

8.reducer:

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).

9.replace() :

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.

10.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.

map() does not execute the function for array elements without values.

this method does not change the original array.

I am a front-end developer. I love to know about new technologies.