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:

"15" - "5"    // 10
"15" / "3"    // 5

The above will work as our strings evaluate to numbers. However,

"5" + "5"   // 55

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ā€.

String(100);     // "100"

And let's convert aĀ BooleanĀ false into aĀ string literalĀ ā€œfalseā€.

String(false);   // "false"

Within a variable, we can convert our value to a string like so:

let name = 5000;       // 5000
name = String(name);   // "5000"

We can check the data type of any value, usingĀ typeof, for example:

typeof name;    // string

Alternatively, we could do this more concisely like so:

let name = 5000;
name.toString();    // "5000"

(5000).toString();  // also returns "5000"

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:

Number("2000");   // 2000

Weā€™ve turned our string ā€œ2000ā€ into the number 2000.

We could also convert a Boolean:

Number(true);    // 1
Number(false);   // 0

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.

Number("one");      // NaN
Number("1,000");    // NaN
Number("1 1");      // NaN
Number("01-01-19"); // NaN

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:

Boolean(100);     // true
Boolean("name");  // true
Boolean(" ");     // true, a space is considered a value

If the value is considered empty (0, anĀ empty string (""),Ā undefined,Ā NaNĀ orĀ null), it will return false:

Boolean(0);         // false
Boolean("");        // false, an empty string (with no space)
Boolean(undefined); // false
Boolean(NaN);       // false
Boolean(null);      // false

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.

Wrapping up

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!

Related Posts:


Tim profile image

A little about me..

Hey, Iā€™m Tim! šŸ‘‹

Iā€™m a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out myĀ Complete Guide to Freelancing if you'd like to find out more.

While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.

Thanks for reading! šŸŽ‰