Type Conversion In Modern Javascript - Part II -
Automatic vs Explicit type conversion in javascript.
Automatic vs Explicit type conversion in javascript.
From the first two examples used in the first part of this series. Here is a link if you missed it, you’ll realize that there are basically two methods of type conversion in Javascript.
From the first example, javascript automatically converted the number to a string which is referred to as automatic type conversion.
console.log(typeof(30 + 'years'));//string
From the second example, we had to introduce an inbuilt javascript function (Number()) to try to convert the string into a number which is referred to as explicit type conversion.
1) Number()
console.log(Number(30 + 'years')); //NaN
This function only works for base 10 integers and does not allow trailing characters. The conversion may not have worked since it was an impossible one, but this function does work where allowed. for example, conversion of a boolean to a number;
console.log(Number(true)); //1
console.log(Number(false)); //0
Here’s is a table that summarizes how primitive type values convert from one type to another in javascript. If you need to learn the difference between primitive and reference types in javascript then have a look at this article.
Other explicit type conversion functions in javascript include;
2) String() / toString()
- String() method attempts to convert the type into a string. Examples;
console.log(typeof(true)); //boolean
console.log(typeof(String(true))); //string
console.log(typeof(1)); //number
console.log(typeof(String(1))); //string
console.log(typeof(true)); //boolean
console.log(typeof(true.toString())); //string
- When using the toString() method the conversion is always in base 10, but if we specify the argument, we can convert into other bases between 2 and 36. Examples;
let age = 100;
console.log(age.toString(2)); //1100100
console.log(age.toString(8)); //144
console.log(age.toString(16)); //64
Apart from null and undefined, all the other types can use the toString() method.
- Javascript does not allow the use of the toString() method on null and undefined types;
let number = null;
number.toString();
console.log(number); //Cannot read property 'toString' of null
let text;
text.toString();
console.log(number); //Cannot read property 'toString' of undefined
3) Boolean()
- Boolean() method attempts to convert the type into boolean. Examples;
console.log(typeof(1)); //number
console.log(typeof(Boolean(1))); //true - boolean
console.log(typeof(0)); //number
console.log(typeof(Boolean(0))); //false - boolean
console.log(Boolean('hello')); //true - boolean
console.log(Boolean('')); //false- boolean
This marks the end of the second part of a series of blog posts related to type conversion in javascript. If you haven't read the first part click this link to get access.
You can share this article on your social media to bookmark it for future reference or give access to your friends also working or interested in javascript language. You can follow me on Twitter , where I share countless resources and articles related to javascript and we can become lifetime friends and javascript lovers.
Finally, THANK YOU so much for taking your time to read this article. If you are feeling more generous I'll not stop you from buying me a cup of coffee.
Until the next article, KEEP CODING & SHARING.