Open In App

JavaScript Type Conversion

Last Updated : 28 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript is a loosely typed language and most of the time JavaScript implicitly converts a value to the right type but there are also cases when we need to explicitly do type conversions. While JavaScript provides numerous ways to convert data from one type to another there are two most common data conversions:

Converting Values to Strings

The JavaScript String() and toString() methods can be used in JavaScript to convert a value to a string. 

Syntax:

String(value) OR  variableName.toString()

Converting Numbers into String

Example: Below code example below implements the String() and toString methods to convert numbers into strings. 

javascript




let v1 = 123;
let v2 = 333;
     
// Conversion of number to string
console.log("Type Of v1 before conversion: " + typeof v1 +
" and Type of v2 before conversion: " + typeof v2);
 
console.log("Type Of v1 after conversion: " + typeof String(v1) +
" and Type of v2 after conversion: " + typeof v2.toString());


Output

Type Of v1 before conversion: number and Type of v2 before conversion: number
Type Of v1 after conversion: string and Type of v2 after conversion: string

Converting Boolean into String

Example: The below code example uses the string conversion methods to change the boolean into string.

Javascript




console.log("Type Of false before conversion: " + typeof false +
" and Type of true before conversion: " + typeof true);
 
// Converting types explicitly to string
console.log("Type Of false after conversion: " + typeof String(false) +
" and Type of true after conversion: " + typeof (true).toString());


Output

Type Of false before conversion: boolean and Type of true before conversion: boolean
Type Of false after conversion: string and Type of true after conversion: string

Converting Date Object into String

Example: The below example will convert the value of date object into string.

Javascript




const currentDate = new Date();
const pastDate = new Date(20/12/2023);
 
console.log("Type Of currentDate before conversion: " + typeof currentDate +
" and Type of pastDate before conversion: " + typeof pastDate);
 
// Converting types explicitly to string
console.log("Type Of currentDate after conversion: " + typeof String(currentDate) +
" and Type of pastDate after conversion: " + typeof (pastDate).toString());


Output

Type Of currentDate before conversion: object and Type of pastDate before conversion: object
Type Of currentDate after conversion: string and Type of pastDate after conversion: string

Converting Values to Numbers

We can use Number() function in JavaScript to convert a value to a Number. It can convert any numerical text and boolean value to a Number. In the case of strings of non-numbers, it will convert it to a NaN(Not a Number)

Syntax:

Number(valueToConvert);

Converting String into Number

Example: Below code converts a numerical text string into a number using Number() method.

javascript




let v = "144";
console.log("Type of v before conversion: " + typeof v);
console.log("Type of v after conversion: " + typeof Number(v));


Output

Type of v before conversion: string
Type of v after conversion: number

Converting Boolean into Numbers

Example: The below code example explains the conversion from boolean type to number type.

Javascript




console.log("Type of true before conversion: " + typeof true);
console.log("Type of true after conversion: " + typeof Number(true));


Output

Type of true before conversion: boolean
Type of true after conversion: number

Converting Date Object into Number

Example: The below code implements the Number() method to convert a date object into number.

Javascript




let d = new Date('1995-12-17T03:24:00');
console.log("Type of d before conversion: " + typeof d);
console.log("Type of d after conversion: " + typeof Number(d));


Output

Type of d before conversion: object
Type of d after conversion: number


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: