### Types in JavaScript

The way in which the JavaScript engine deals with types is called *Dynamic* *Typing*.

It is based on the fact that there is no need to provide information about the type of variable that we want to create.

Variables can hold diferent types of values because just like i said its figured out during execution phase.

It should also be remembered that there in other languages may be the opposite of *Dynamic* *Typing* - *Static* *Typing*.

An example of such language is Java.

In the first line, we see how the Java engine would react to assigning to an integer variable a string.

In JavaScript, however, despite its pluses and minuses, we would not get an error.

### Primitive Types

JavaScript has 6 *Primitive* *Types*. But what is *Primivite* *Type* really?

It is a type of data that represents a single value. Type of data that is not an object.

Let's start with the first one.

*undefined*

As you remember, we have already come across this. It represents lack of existence but you shuld not set a variable to this. If you want to set something to equal nothing you should use a second *Primitive* *Type*: *null*.

*null*

It also represents lack of existence but it's used by developers when they want to set for example a variable to equal nothing.

*boolean*

It is represented by two values: *true* / *false*.

*number*

Unlike other programming languages, there is only one number type and it's a *Floating* *Point* *Number*. It means that there are always some decimals. You will see in the future that it can make math a little bit weird.

*string*

It is a sequence of characters placed in *" "* or *' '*.

*symbol*

This one is new and is available from ES6 (next version of JavaScript).

At the moment, I will leave it as it is, but it is good to know that there is such a type.

### Operators

*Operators* in JavaScript are just a special functions that are syntactically different. Generally, *Operators* take two parameters and return one.

Above we have assigned to the variable the result of adding two numbers, *1* and *5*. Everyone knows that this will result 6. However how does JavaScript know it? As I mentioned above, the operators are really just the functions, and if so, our case could be represented in the following way.

However, it would be very troublesome and not very intuitive. After all, we have all been taught mathematics since the early years and we know that if we want to add two numbers, we do it obviously: *1 + 5*, not *add(1.5)*.

Therefore, JavaScript and other programming languages provide what we call infix notation. Infix means that the function name, the operator, in our case the plus operator is placed between two parameters.

To get to know all kinds of operators, please visit this link. There, you will find a detailed breakdown of operators and simple examples.

When you are finished, and i strongly recommend since it is really well explained we will dive into two topics related to operators.

### Operator Precedence

It tells us which operator function gets called first. Functions (remember operators are just a special type of a function) are called in order of precedence. A higher precedence always wins.

### Associativity

It tells us in what order operator functions get called in: *left-to-right* or *right-to-left*. It happens when functions have the same *operator precedence*.

Ok let's check this example.

Here is a simple piece of code. When it comes to JavaScript, you should know that the math rules are also valid in JavaScript. And since JavaScript is synchronous, this means that we will not add 2 to 2 at the beginning or the whole equation will not be done at the same time. I will give you another link that might help. You will find there a table that looks like this:

It shows a value of *precedence* and an *associativity*. So when it comes to *Operator* *Precedence* you must remember that the operator with higher value will **always** win.

Together with the *Precedent* *Operator* goes *associativity*. As you can see in the table above, you can choose either *left-to-right* or *right-to-left*. To better understand when is it useful to us, I will use an example.

Do you have an idea what will be the result of each console.log?

Why is it like that ? The answer is *Associativity*.

In the *a = b = c* statement, we see two identical operators. Since the operators are same it means they have the same *Operator* *Precedence* value. Let's take a look at the table now. When we find equal symbol, we see its *precedence* is equal to 3 and its *associativity* is *right-to-left*.

This means that in our* a = b = c *statement, JavaScript engine will start with *b = c* part. Since the operator is also an equal sign, associativity is *right-to-left* again. Therefor b = 4.

If *b* is equal *4* now and last part of the statement is *a = b*, a will also equal to *4*. That's why the result of all three *console.log* methods will be 4.

### Next Post

I will talk about *Coercion* and a little bit more about *Comparison Operators*.

## Comments