27 julio, 2024

Declare variables in JavaScript | Bootcamps

JavaScript is a complex programming language that allows us to develop both for the world frontend as for the world backend of the web. Despite its complexity, JavaScript acts like any other programming language in that it allows us to communicate with the machine through a series of rules known as syntax.

Just like loops, variables are a very important element within the JavaScript syntax. Therefore, in this post, we will teach you how to declare variables in JavaScript using three keywords.

What is a variable in JavaScript?

Before teaching you how to declare variables in JavaScript, we need to understand the element we are talking about. A variable in JavaScript is this programming language’s way of naming a value. That is, when we declare a variable, we assign a name to a certain function. This allows us to not have to repeat this value again, but rather insert the name when we want to use it. Furthermore, this facilitates the action of changing this value at a global level, since It is enough to change the value in the variable so that all the values ​​in which this variable is inserted are altered.

Now that you know what a variable is, we explain how to declare variables in JavaScript.

How to declare variables in JavaScript?

There are three ways to declare variables in JavaScripteach one uses a different keyword: let, var and const. Below, we explain how each of these keywords works:

var

The key word var is an abbreviation of variable and it’s the old way of declaring that what we’re writing is this type of element. To use this keyword, we can write the following line of code:

var x=1

Once we have declared that x It is a variable and it is okay 1, We do not need to put this variable again if we want to give it a new assignment. This happens because, when declaring variables in JavaScript using the keyword var, we are using a global context, also known as function scoped. This means that, from the moment we have defined the variable, it will be true in all our code.

let

The key word letunlike the keyword var, allows us to declare variables in JavaScript that are true at the block level. This is also known in English as block scoped. So this makes variables defined with let are only true within a block, introduced by the braces { }.

To better understand this difference between function scoped and block scopedLet’s take an example. Let’s say we define the following two variables:

var x = 1;

let y = 1;

Now, we decided to change these two variables within a block, like the one we have below:

if (true) {

var x = 2;

let y =2;

}

So, although the variables have undergone the same change, when we want to paint the elements x and and, we will obtain different results. when we paint console.log(x), we will get number 2; while when we paint console.log (y)we will get the number 1. This happens because the variable let that we defined in the previous block is only true within the block, so your change is not applied generally.

For its part, the variable var It has a general effect, so any change made to the code, whether within a block or not, will be represented in the final result.

The difference between these two keywords allows us to create variables that respect a certain hierarchy in our code. So, when using the keyword let, we respect what the previous block has defined, while the keyword var It generally assumes the last thing we have written. We can see this directly in our software programming, since the variable and In the second block it will be represented in a more opaque way, since it will not have any real effect. The variable x It will keep the brightness of the rest of the code, because it is being applied.

const

Like the keyword let, const allows us to set variables at the block level. The difference between these two keywords is that const refers to a constant. So, suppose we write the following line of code:

const i=7

This means that we have a constant where Yo is equal to 7. If we now decide to change the value of Yo inside the block, JavaScript will return an error. This happens because the value of Yo is defined as a constant, a type of immutable variable.

Do you dare to continue learning?

After reading this post, you know exactly how to declare variables in JavaScript using the three keywords we have taught you. However, JavaScript is a broad programming language that still has a lot to teach us. Therefore, we invite you to continue learning with us in our Full Stack Web Development Bootcamp. There you will learn, among many other things, everything you need to develop web pages using JavaScript and you will create your own lines of code to demonstrate the skills learned. Don’t hesitate and sign up now to become an expert in the IT sector!

Deja una respuesta

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