JavaScript Data Type Conversion Tutorial
Tags: javascript, beginners, programming, tutorial
Published: 2022-07-09 18:20:39

## Converting Data Types
As we’ve seen in the previous article, data types are classifications of a particular type of data. And it’s the type that determines which operations are able to be performed on the data. As we write programs, we’ll often need to convert our data types in order to perform tasks.

JavaScript is intelligent enough to convert some values for us, this is known as _type coercion_:
![Converting Data Types](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89yromwgt5rxqn5ttq4x.png)
The above will work as our strings evaluate to numbers. However,
![Converting Data Type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxpw5iqcnacvxi123c76.png)
If we’re using the `+` operator, the strings will concatenate! So if we attempted to rely on type coercion in this case, we’d see unexpected results.

For this reason, when writing our own code, we should endeavor to convert data types ourselves — thus reducing potential errors.

Let's now take a look at how we can go about that!

### Converting Values to Strings
Using the `String()` method, we can explicitly convert values to strings.

For example, let's take the _number_ 100 and convert it to a `string literal` “100”.
![Values to Strings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufhgjh3lmhyzxht7yigi.png)
And let's convert a `Boolean` false into a `string literal` “false”.
![String False](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eoaad2enedopy2auh0m9.png)
Within a variable, we can convert our value to a string like so:
![String Name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70v6sztxuzwp9upf5op6.png)
We can check the data type of any value, using `typeof`, for example:
![Typeof Name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n254styrlheduwxa56pa.png)
Alternatively, we could do this more concisely like so:
![Javascript Strings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uorf8071wdfynmvd8rqv.png)
Regardless of the method we choose, by using `String()` or `n.toString()`, we can explicitly convert our data into string values.

### Converting Values to Numbers
When the time comes to convert values into numbers, we use the `Number()` method in a similar manner:
![Coverting Values to Numbers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8at30zvtgqym0ge0opu.png)
We’ve turned our string “2000” into the number 2000.

We could also convert a Boolean:
![Convert a Boolean](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uch3trqomzoumjm73uji.png)
It should be noted however, that we cannot convert our data type into a number if there are any characters or spaces within the string! If this were to be attempted a `NaN` (not a number) would be returned.
![Convert to Nan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmvjnycm8ksmtkk9k56g.png)

### Converting Values to Booleans
We use `Boolean()` to convert strings and numbers into Boolean.

If **any** value is present, it will be converted to `true`:
![Converting Values to Booleans](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lilfkmw4iuj4j27lh1hb.png)
If the value is considered empty (`0`, an `empty string ("")`, `undefined`, `NaN` or `null`), it will return false:
![Booleans](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ixm9q0zgfytzn685g2n.png)
Converting numbers and strings into Boolean values is a powerful way to start to introduce logic into our programming. For instance, we could detect a missed required field on an input form, if the Boolean returns false.

## Summary
That's all for today!

In this article, we looked at how we can work with data types, using both type coercion as well as by implicitly performing our own type conversions.

In the next one, we'll dive right into working with strings!

## Conclusion
If you liked this blog post, follow me on [Twitter](https://twitter.com/RembertDesigns) where I post daily about Tech related things!
![Buy Me A Coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijinng8k7bf23v0o6gkj.png) If you enjoyed this article & would like to leave a tip — click [here](https://www.buymeacoffee.com/rembertdesigns)

### 🌎 Let's Connect

- [Portfolio](https://www.rembertdesigns.co/)

- [Twitter](https://twitter.com/RembertDesigns)

- [Github](https://github.com/rembertdesigns)

- [Codepen](https://codepen.io/rembertdesigns)

- [LinkedIn](https://www.linkedin.com/in/rrembert/)