Concepts to grasp when programming JavaScript

Tram Ho

Programming with JavaScript every day but do you know, behind the code, how JS is working? This article will cover the concepts you need to grasp when programming with JavaScript.

1. Scope

  • The place where you access your declarations is called scope
  • There are two types of Scope: Global Scope and Local Scope . The difference between them is that for Global Scope, you can access declarations from anywhere in your code, and for Local Scope, you can only access them inside that block of code ( the block of your code ). can be understood as in brackets {} ). An illustrative example of this.
  • Through the above example you can understand more about the code block and the differences between Global Scope and Local Scope.
  • Note: You need to pay attention to the difference between the two types of variable declaration var and let about scope (will be mentioned later).

2. Hoisting

  • This is the mechanism of JavaScript that allows implementation of components before they are declared. For example you can call Function before defining it.

As in the structure above, JS will process the code as follows:

  1. Hoisting
  2. Executing the code inside the function While running the code will be reorganized like this:

3. IIFE

IIFE stands for Immediately Invoked Function Expression It means that when you declare a function, the function will be called immediately. This allows the creation of private variables and functions.

  • You see that the function call operator () is written right after the declaration

4. Currying

Treat the function as a variable passed in the argument

  • More specifically, a function is treated as a variable. image.png

5. Difference between let and var

  • You have to learn the concept of scope above to understand this image.png
  • When declared with let, a is null when using block scope
  • When declared with var, it is 1 because of using function scope image.png

6. Conclusion.

A few more concepts will be covered in the next section.

Share the news now

Source : Viblo