DEV Community

Zhanna Balyan
Zhanna Balyan

Posted on

Java Script variables

Hey everyone. Welcome back to my blog. Last time we talked about binary numbers and operations but this time we are going to go a little deeper and talk about programming languages, in particular Java Script. As we all know there are several programming languages, which in simpler words, are tools for people to communicate with computers. One of such programming languages is Java Script. It is a dynamic, lightweight, object-oriented language. Originally, it was designed by a programmer named Brendan Eich for usage in web browsers. But now it is used in several fields. For example, with the help of Java Script people can develop web and mobile apps, create games, build servers etc. It is one of the most popular languages as it is easy to read, is versatile and there is a high demand for it. So much for the introduction, now let’s dive deeper into the main topic and understand how Java Script works.

In Java Script, there are different data types. We have strings, arrays, objects, functions and so on. Like any other programming languages, Java Script has variables. Those are simply places for storing data. Variables are the most fundamental and important parts of programming languages. We can put pretty much anything in variables ranging from just numbers and texts to functions and other complex data. We can reference the data inside the variable numerous times. Furthermore, we can later modify or reassign a different value to it. There are three ways to declare variables. We do that with the help of keywords “var”, “let” and “const”. The code understands the variable differently in each case. In the past, we only used var for declaring variables and then we got introduced to let and const. We use var to describe the variable. We write the name of the variable next to var. Then we assign the variable by putting the “=” sign. Next we write the value that needs to be assigned. There are some restrictions that we need to follow. For example we can’t use spaces or tabs. The value assigned to the variable needs to be put inside parenthesis. Also, the variable needs to have a unique name. Let’s look at a simple example to understand how variables are written.

Image description

We can see that our variable is named animal and its value is dog.

Now if we print the variable with the help of console.log, which is a function used for outputting messages, it will print “dog”.

Var and Let are somewhat similar but the difference between them is that var is function-scoped and let is block-scoped. Scopes in Java Script refer to the visibility or accessibility of the variable. In other words, they determine where the variable can be used. There are two scopes, global and local. Global variables can be redeclared outside of a block, meanwhile local variables are declared inside of a block. And this is the key difference between let and var. Let allows us to declare variables which are limited to a block statement but var lets us redeclare the variables. So the let variables need to be declared before use. The example below will show exactly what I’m talking about.

Image description

As we can see, the variable X can not be used outside of the curly brackets.

Now that we know what var and let are, there is only one last type of variable that we need to define. It is const. Constants are values that can not be reassigned or changed. We write them just like the other two variables. First, we write the constant’s name and then its value. This is shown in the example below

Image description

So, to sum up, we’ve learned the fundamental and basic things about Java Script and data types used there. Now that you have this information, you can have fun trying to code something yourself. Thank you for reading.

Top comments (0)