What’s we need to know about JavaScript type conversions?

Mehedi hasan
2 min readNov 3, 2020

JavaScript is lightweight , interpreted , object-oriented programming language with first-class function for web pages. In programming we need to store data. Each data has own data type like string, number, array etc. Sometimes for coding purpose we want to conversion this datatype. So, in this article we learn how to conversion data types with each other in JavaScript.

  1. number to string: Any kinds of number(integer, decimal) without wrapping in quotation is called number data type. There is a simplest way to convert number to string is wrapped number with (‘ ‘) or (‘’ ‘’) quotation. Some other way to convert this type. So, let’s start to explore them.

a) String function: numbers wrapped with String function converted number to string.

let example;example = String(55);console.log(typeof example);

Some interesting things happened here. Suppose, when we want to concat some number with quotation it’s concat like

let example;example = ‘5’ + ‘5’;console.log( example);console.log(typeof example);

But with String function it’s concat with arithmetic expression.

let example;example =String( 5+ 5);console.log( example);console.log(typeof example);

b) toString(): another way to convert numbet to string is toString() method.

let example;example = 4 + 4;console.log(example.toString());console.log(typeof example);

2. date to string: By default date is an object data type. If you want to convert date to string, String function helped you.

let example;example =String(new Date());console.log( example);console.log(typeof example);

3. boolean to string: true/false are called boolean value. To convert them into string, String function is used .

let example;example =String(true);console.log( example);console.log(typeof example);

4. array to string: multiple data within single variable is called array in JavaScript. Array is a collection of data. To convert them with string, String function is helpful.

let example;example =String(true);console.log( example);console.log(typeof example);

5. string to number: Any kinds of value wrapped with quotation is called string. To convert them with number, Number function is used.

let example;example =Number(“78768”);console.log(example);console.log(typeof example);

When we want to string(pure text) to number, it’s define NaN(not a number).NaN is a number data type.

let example;example =Number(“write more blogs on medium.com”);console.log(example);console.log(typeof example);

6. boolean to number: true/false known as boolean value. In computer language true defines as 1 and false defines as 0. Convert boolean to number, Number function is used.

let example;example =Number(true);console.log(example);console.log(typeof example);

7. boolean to string: toString() method used returns a string representing the specified boolean object.

const flag = new Boolean(1);console.log(typeof flag.toString())

--

--