26 julio, 2024

What is JavaScript hoisting? | Bootcamps

Variables are a fundamental part of the syntax of many programming languages, as they allow us to declare elements that we will use in our code. In this post, we will teach you What is the concept and how does it work? hoisting JavaScriptessential to understand the behavior of variables in this programming language.

Before starting

When we talk about hoisting JavaScript, we refer to a concept that affects the way variables behave in this programming language. As you can read in our post about declaring variables in JavaScript, There are three ways to define a variable: var, let and const.

Why are there three ways and not just one? Because Each keyword allows us to determine a different behavior. In this sense, the word we use the most will reflect the way in which we have decided to program.

So, for example, a web developer who prefers to execute the logic of his projects by nesting independent functions, will use const as your preferred variable declaration. This is because this logic requires establishing constants instead of variables that mutate with the passage of lines. This also allows more readability in the code by giving clarity about the behavior of the variable in the future, since it will not change. This readability is something any web developer will appreciate.

When to use a variable declared with let? From we recommend that you use this keyword when you know that the variable can transform its valueas would be the case of a variable that can take the value of null or that starts in this state and is later modified. In the context of large code, these types of variables stand out and call the developer to pay more attention to the values ​​they declare.

In recent years we have seen a decrease in the use of var as a way to declare variables. This is deeply related to the concept of hoisting of JavaScript.

What is JavaScript hoisting?

The concept of hoisting JavaScript determines that the value of a variable declared with var can rise to the beginning of the scope of the function within which it is declared. This can be dangerous, because you can end up with a value of type undefined despite having given value to your variables. That is, the keyword var sometimes it surprises us with undefined values ​​in defined variables.

He hoisting JavaScript rarely generates this behavior in variables declared with const and with let. Although it can happen, the web developer almost has to generate this behavior on purpose for it to happen. This is why, in recent years, web developers focus much more on these two keywords, so that he hoisting JavaScript does not accept variables negatively.

We hope this post has resolved your doubts. If you want to go deeper into the concept of hoisting of JavaScript, we invite you to read the article hoisting on the official page of the Mozilla Developer Network.

Whats Next?

After reading this post, you know exactly what it is and how it works. hoisting of JavaScript. However, There is still a lot to learn! For this reason, we recommend our Full Stack Web Development Bootcamp, an intensive training space where you will learn the concepts, programming languages ​​and fundamental tools to develop on the web. Dare to continue learning and sign up now!

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *