Converting value types in JavaScript

Stela Capsa
3 min readOct 12, 2021

Type conversion is when we explicitly convert type to another.

For example:

const birthYear= "1991";

In order to be able to convert this string in to a number, in JS ,we have to do it with following function.

console.log(Number(birthYear))

Now well define a variable with value a number:

const birthYear= 1991;

In order to convert a number to a string, we have to do it with the next function:

console.log(String(birthYear))

Most of the time JavaScript does conversion automatically, and we don’t have to do it manually.

Type coercion it happens whenever an operator is dealing with two values that have different types, so than js will convert one type operation to match the other. As an example:

The next example is an important thing to remember in order to avoid bugs or confusions.

When we subtract string and numbers in one operation in JS, everything works fine. What do you think will happen when we’ll do the adding operation?

JavaScript is concatenating the numbers and strings if is add operation, and the result is converted in to a string.

Type Boolean is truthy or falsy value. Total there are 5 types of falsy values.

Let’s see an example of falsy value.

and if to replace age with any number will come up with a truthy value

Happy coding !

--

--