The community is working on translating this tutorial into Korean, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Type conversion
As we already talked about, JavaScript is a weakly typed, dynamic language. In other words, it doesn't require that you specify a type when declaring a variable - instead, it will look at what you assign to the variable and then treat it accordingly, behind the scenes.
This also means that in most cases, you don't have to worry about types. Just declare a variable, assign a string to it, and JavaScript will treat it as a string. If you change your mind later on, you can just assign a number to it, and JavaScript will now treat it as a number.
But this doesn't mean that JavaScript doesn't care about types, because internally, types still matter a great deal, and sometimes, it will matter to you as well. For these situations, you can manually convert between the primitive types as you see fit, to ensure that a certain variable is treated as you would expect it to be treated.
In this article, we'll be discussing type conversions and how you can use them to convert between various types.
Convert to boolean
JavaScript will use type coercion, as discussed in the article on Truthy and Falsy, to automatically convert various values into true or false (a boolean), when needed. However, sometimes you want a real boolean value, and for this, we have the Boolean() function, which will try to convert the given input into a boolean (true or false). In its most basic form, you can convert a number, e.g. 0 or 1, to a boolean:
alert(Boolean(0) === true); // false
alert(Boolean(1) === true); // true
But you can also supply it with less obvious values, like strings. In this case, JavaScript will generally convert things that could be considered as "empty" or undefined into false, while actual, non-empty values will be converted into true. Here's a couple of examples:
alert(Boolean("") === true); // false
alert(Boolean(null) === true); // false
alert(Boolean(undefined) === true); // false
alert(Boolean("hello") === true); // true
alert(Boolean("000") === true); // true
Convert to number
Sometimes you need to ensure that you're dealing with numeric values. For instance, if you take input from the user, e.g. through the browser-based function prompt(), you will get a string back, even if a number is entered. This will lead to unexpected results if you try to do math with the input, as illustrated by this example:
let n1 = 40;
// Example input: 2
let n2 = prompt("Please enter a number:");
// Example output: 402
alert(n1 + n2);
Because the prompt method always returns a string, when we add the two variables, JavaScript will do string concatenation instead of actual math. So, if you enter "2" in the prompt, the result will be the string "402" instead of the number 42. So, if we want to make sure that the input is treated as a number, we need to convert it and we can do that using the Number() function:
let n1 = 40;
// Example input: 2
let n2 = Number(prompt("Please enter a number:"));
// Example output: 42
alert(n1 + n2);
The Number() function is quite flexible. Besides strings, it can also take booleans and values like undefined and null and it will even try to clean any strings for unrelated characters like spaces, tabs, newlines etc. If it can't turn the input into a numeric value, it will return NaN (Not a Number). Here are some examples of how it works:
// 0
alert(Number(null));
// 1
alert(Number(true));
// 42
alert(Number(" 042\t\n "));
// NaN
alert(Number("forty-two"));
Convert to string
There can be situations where you want to ensure that you are working with strings as well. This is usually not a problem, because most types can be easily converted into strings using the String() function.
The result of the String() function is quite easy to guess, because it will simply be a string representation of whatever you feed into it, including values like null, undefined etc. Here are some examples:
// 42
alert(String(42));
// true
alert(String(true));
// null
alert(String(null));
let n1 = String(40);
let n2 = String(2);
// 402
alert(n1 + n2);
Pay special attention to the last one, where you can clearly see that we have converted our numbers into strings in the way that they are concatenated ("402") instead of added (42).
Summary
Because JavaScript is such a dynamic and flexible language when it comes to types, you rarely have to worry about them. However, in some situations, you need to ensure that you are working with a specific type. Luckily for us, it's easy to convert between the primitive data types of JavaScript, as we have seen in this article.