JS 101 (3 - n) Type Conversion

Tags: codenewbie, beginners, webdev, javascript
Published: 2021-01-09 22:08:20

**I wish all readers a happy new year!**

I'll pick up the series I started last year and today we'll talk about type conversion. If you want to read the first episodes of this series, here is this table of contents.

<div class="table-wrapper-paragraph"><table>
<thead>
<tr>
<th><center> Content </center></th>
</tr>
</thead>
<tbody>
<tr>
<td><center><a href="https://dev.to/vplentinax/js-101-i-n-fundamentals-3cg5"><strong>JS 101 (1-n)</strong> - Introduction</a></center></td>
</tr>
<tr>
<td><center><a href="https://dev.to/vplentinax/js-101-2-n-fundamentals-25l3"><strong>JS 101 (2-n)</strong> - Data types</a></center></td>
</tr>
<tr>
<td><center><a href="https://dev.to/vplentinax/js-101-4-n-fundamentals-92"><strong>JS 101 (4-n)</strong> - Comparision</a></center></td>
</tr>
</tbody>
</table></div>

In the previous post we learned about the types of data that we can find in this programming language.

In many cases, we will need to handle a specific type of data that perhaps, in its first instance, was declared as another type of data. These situations mainly occur when the data that our users enter into our system is handled. To control these situations, both in javascript and in many other languages, there is what is known as "type conversion".

## **Type Conversion**
This conversion operation is quite simple in this language. The Javascript interpreter automatically converts the data to the data specified in its declaration.

For example, adding double/single quotes will interpret the content as a string type. If they are numbers as a numeric type. But sometimes, we will need to do an explicit conversion. Let's see how we can do it.

### **To String**

To convert a data into a String, you just need to use the `String (value)` function. 

<img src="https://i.ibb.co/Y35SHfh/string.png" alt="string" border="0">


### **To Number**

There are several ways to convert data to number, including the `Number(value)` function, the `parseInt(value, base)` function, the `parseFloat(value)` function and the unary operator `+`. In this post we will see the conversion using the `Number(value)` function and we will talk about the others in another occasion.

#### String to Number

<img src="https://i.ibb.co/gRTy9Q1/num1.png" alt="num1" border="0">

#### Boolean to Number

<img src="https://i.ibb.co/vBTDwYt/num2.png" alt="num2" border="0">

#### Undefined and null

<img src="https://i.ibb.co/JQFxf0T/num3.png" alt="num3" border="0">

#### Mathematical operations and expressions
On this occasion, the javascript interpreter converts the numerical strings into mathematical operations directly into numbers, solving the operations.

<img src="https://i.ibb.co/RPPXPv7/operations1.png" alt="operations1" border="0">

But the same does not happen with addition when mixing strings and numbers.

#### Addition, concatenation, or unary operator
The symbol `+` can mean three things in the programming world, two of them can be an **addition operation** or **a concatenation operation**. In a conversion to Number using the `Number(value)` function, it works as **concatenation** if at least one of the operators is a string of numeric value.

<img src="https://i.ibb.co/qrYFWyf/oper2.png" alt="oper2" border="0">

However, this same symbol is a **unary operator**, which used, as its name indicates, with a single operands, and located on the left side, works as an abbreviation for the `Number(value)` function, since it also converts to numbers . Let's look at an example.

<img src="https://i.ibb.co/swfKXTG/unary.png" alt="unary" border="0">

There is a possibility that the result in both ways is different. In this table they shared on [StackOverflow](https://i.stack.imgur.com/QNMfI.png) you can compare both ways.

## **To Boolean**
To convert a data into a Boolean, you just need to use the `Boolean(value)` function. 

<img src="https://i.ibb.co/VC9qYLv/bool.png" alt="bool" border="0">

As we can see, the conversion operation is not difficult, and its rules are easy to remember.

I hope you liked this post. Don't miss the next one. See you soon!

If you want to read more about Javascript:

- [Java vs Javascript] (https://dev.to/vplentinax/java-vs-javascript-4hbd)
- [Javascript behind scenes](https://dev.to/vplentinax/javascript-behind-scenes-1d9)

If you want to read about other topics:
- [Classification of computer languages ​​(I)](https://dev.to/vplentinax/computer-languages-i-24pb)
- [Software development models and methodologies (Part 1)] (https://dev.to/vplentinax/software-development-models-and-methodologies-part-1-1kl5)
- [Algorithm](https://dev.to/vplentinax/algorithm-1ao0)
- [Suggestion for developing web pages with HTML](https://dev.to/vplentinax/suggestion-for-developing-web-pages-with-html-3fam)

Follow me!
[Instagram](https://www.instagram.com/vplentinax/)
[Twitter](https://twitter.com/vplentinax)