Types of variables used in JavaScript
Like any language, JavaScript allows you to create variables that will be used to manipulate data. JavaScript is a very simple language so, for example, data types are very basic. We will thus have the following as the main data types:
- Numerical values
- Boolean values
- Character strings
- Arrays
- Objects
Let’s take a quick look at these different types of data.
Numerical values
Numerical values can be positive or negative and even in decimal form (for example, 0, -10, 10.45). All mathematical numbers called real numbers comprise numerical values or data points.
Boolean values
These are of course the two Boolean values—true or false—that are found in most languages. These values are used to express conditions: if the condition is true, then we perform a particular process, otherwise, we perform an alternative one. The result of the condition is therefore a true or false value, which is symbolized by the two values true
and false
.
We will see how to express conditions in the Writing conditions section, later in this chapter.
Character strings
Character strings refer to values such as "a"
, "abc"
, or "Hello, how are you?"
. An empty character string will be represented by ""
(consecutive quotes with nothing inside). Note that you can use double quotes ("
) or single quotes ('
). Thus, the string "abc"
can also be written as 'abc'
(with single quotes).
Arrays
Arrays, such as [10, "abc", -36]
, can contain values of any type, like here where we have both numeric values and character strings. An empty array will be represented by []
, which means that it contains no value.
The values stored in an array are accessed by means of an index, varying from 0 (to access the first element placed in the array) to the length of the array minus 1 (to access the last element of the array). So, if the array [10, "abc", -36]
is represented, for example, by the variable tab
, the following occurs:
tab[0]
will allow access to the first element of the array:10
.tab[1]
will allow access to the second element of the array:"abc"
.tab[2]
will allow access to the third and last element of the array:-36
.Note
Note that it is possible to add elements to an array, even if it is empty. So, if we access index 3 of the previous array
tab
, we can writetab[3] = "def"
. The arraytab
will therefore now be[10, "abc", -36, "def"]
.
Objects
Objects are similar to arrays. They are used to store arbitrary information, for example, the values 43
, "Clinton"
, and "Bill"
. But unlike arrays that use indexes, you must specify a name to access each of these values. This name is called the key, which thus allows access to the value it represents.
Let’s suppose that the previous value 43
is that of a person’s age, while "Clinton"
is their last name, and "Bill"
is their first name. We would then write the object in the following form: { age: 43, lastname: "Clinton", firstname: "Bill" }
. The definition of the object is done by means of braces, and what is indicated inside is pairs of data of the form key: value
separated by commas. This writing format is also called JavaScript Object Notation (JSON) format.
So, if the previous object is associated with the variable person
, we can access their age by writing person["age"]
(which will therefore be 43
here), but we can also write person.age
, which will also be 43
. Similarly, we can also write person.lastname
or person["lastname"]
and person.firstname
or person["firstname"]
to access the person’s last name and first name, respectively.
The key is also called a property of the object. Thus, the age
key is also called the age
property. We can choose any name for the key; you just have to indicate the key and then use it under this name. So, if you specify age
as a property in the person
object, you must use the term age
in the expressions person.age
or person["age"]
; otherwise it will not work.
Note that if you write person[age]
instead of person["age"]
, JavaScript considers age
to be a variable with a previously defined value, which it is not here and therefore cannot work in this case. You would have to set the age
variable to have the value "age"
for this to work.
The elements of an array are ordered according to their index (starting from 0, then 1, and so on), while the elements contained in an object are ordered according to the keys indicated for each element. But even though the lastname
key is listed in the person
object before the firstname
key, this does not differentiate the object { age: 43, lastname: "Clinton", firstname: "Bill" }
from the object { firstname: "Bill", lastname: "Clinton", age: 43 }
because the order in which keys are written to an object is irrelevant.
Finally, there are empty objects, such as those containing no key (therefore no value). We write an empty object in the form { }
, indicating nothing is inside. We can then add one or more keys to an object, even if it is initially empty.
Now that we have seen the main variable types used in JavaScript, let’s see how to use them to define variables in our programs.