Variables in Javascript

We cover variables in Javascript in detail. We will see in general terms what a variable is in programming languages. Then we’ll see how variables work specifically in the Javascript language.

This is the first of the articles that we are going to dedicate to variables in Javascript within the . We will see, if we don’t already know, that variables are one of the fundamental elements when creating programs, in Javascript as well as in most existing programming languages.

Let’s start then by getting to know the concept of variables and we will learn how to declare them in Javascript, along with detailed explanations on their use in the language.

variable concept

A variable is a space in memory where data is stored, a space where we can save any type of information we need to carry out the actions of our programs. We can think of it as a box, where we store data. That box has a name, so that later we can refer to the variable, retrieve the data as well as assign a value to the variable whenever we want.

For example, if our program performs additions, it will be very normal for us to store the different addends that participate in the operation and the result of the addition in variables. The effect would be something like this.

adding1 = 23 adding2 = 33 sum = adding1 + adding2

In this example we have three variables, adding1, adding2 and sum, where we store the result. We see that their use for us is as if we had a section where we can save data and that they can be accessed by simply putting their name.

See also  New Photoshop CC 2015

Rules for variable naming in Javascript

Variable names must be constructed with alphanumeric characters (numbers and letters), the underscore or underscore character (_) and the dollar character $. Apart from this, there are a number of additional rules for constructing names for variables. The most important is that they cannot start with a numeric character. We cannot use rare characters such as the + sign, a space or a – sign. Allowed names for variables could be:

Age

country of birth

_Name

$item

Other$_Names

We must also avoid using reserved names as variables, for example we will not be able to call our variable words like return or for, which we will see are used for structures of the language itself. Now let’s see some variable names that are not allowed to be used:

12 months

your name

return

for

about

pe%pe

Variable names in Javascript are case sensitive.

Remember that Javascript is a case sensitive language, so variables are also affected by that distinction. Therefore, the name variable “myname” is not the same as the variable “myName”. “Age” is not the same as “age”.

Take this detail into account, since it is a common source of code problems that are sometimes difficult to detect. This is because sometimes you think that you are using a variable, which should have a certain data, but if you write it wrong and put upper or lower case where it should not, then it will be a different variable, which will not have the expected data. Since Javascript does not force you to declare variables, the program will execute without producing an error, however, the execution will not produce the desired effects.

See also  Restore a backup or MySQL database backup

Declaration of variables in Javascript

Declaring variables consists of defining, and incidentally informing the system, that you are going to use a variable. It is a common practice in programming languages ​​to explicitly specify the variables to be used in programs. In many programming languages ​​there are strict rules when it comes to declaring variables, but the truth is that Javascript is quite permissive.

Javascript skips many rules because it is a somewhat free language when it comes to programming and one of the cases in which it grants a bit of freedom is when declaring variables, since we are not forced to do so, contrary to what happens in other programming languages ​​like Java, C, C# and many others.

Declaration of variables with var

Javascript has the word “var” that we will use when we want to declare one or more variables. Naturally, that word is used to define the variable before using it.

Note: Although Javascript does not force us to explicitly declare variables, it is advisable to declare them before using them and we will see later that it is also a good practice. In addition, in subsequent articles we will see that in some special cases, a script in which we have declared a variable and another in which we have not, will not produce exactly the same results, since the declaration or does not affect the . var operand1 var operand2

You can also assign a value to the variable when it is being declared

var operand1 = 23 var operand2 = 33

It is also allowed to declare multiple variables on the same line, as long as they are separated by commas.

See also  events in javascript

var operand1,operand2

Declaration of Javascript variables with let and const

From Javascript in modern versions (remember that Javascript is a standard and that as such it evolves over time), specifically in Javascript in its ES6 version, there are other ways of declaring variables:

let declaration: This new way of declaring variables affects their scope, since they are local to the block where they are being declared.

Const Declaration: Actually “const” does not declare a variable but a constant, which cannot change its value throughout the execution of a program.

Perhaps at this point in the Javascript manual it is not necessary to go too deep into these declaration models and for people who are just learning we recommend focusing on the use of “var” declarations. However, if you want to have more information about these new types of variables, we explain them in the article. If you want to see other new features of the Javascript standard that came in 2015, we recommend reading the .

In the next article we will continue learning about variables in Javascript and we will see one of the most important concepts that we must learn about them, the .

Loading Facebook Comments ...
Loading Disqus Comments ...