Type Conversion in JavaScript: How to Convert and Cast Data Types


In JavaScript, type conversion is the process of changing the data type of a variable or a value. This can be done using various built-in functions and methods.

Few Examples of Type Conversion
  • Strings to Numbers
  • Numbers to Strings
  • Dates to Numbers
  • Numbers to Dates
  • Boolean to Numbers
  • Numbers to Boolean

Type conversion Methods

  • String(value) : Converts the given value to a string.
  • Number(value) : Converts the given value to a number.
  • Boolean(value) : Converts the given value to a boolean.
  • parseInt(value) : Converts the given value to an integer.
  • parseFloat(value) : Converts the given value to a floating-point number.

JavaScript also has some unary operators that perform type conversion

  • +value : Converts the given value to a number.
  • -value : Converts the given value to a number.
  • !value : Converts the given value to a boolean.

It is also possible to convert a value to a different type using the valueOf() and toString() methods.

JavaScript also has some automatic type coercion which happens when different types are being used together in an operation. For instance, if a string is added to a number, JavaScript will convert the string to a number before performing the addition.

It's important to keep in mind that type conversion can lead to unexpected results if not handled properly.

Source Code

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

script.js
//Type Conversion
 
let a;
//Others to String
a=25;
console.log(a,typeof a);
a=String(25);
console.log(a,typeof a);
 
a=25.5;
console.log(a,typeof a);
a=String(25.5);
console.log(a,typeof a);
 
a=true;
console.log(a,typeof a);
a=String(true);
console.log(a,typeof a);
 
a=new Date();
console.log(a,typeof a);
a=String(a);
console.log(a,typeof a);
 
a=[1,2,3,4,5]
console.log(a,typeof a);
a=String(a);
console.log(a,typeof a);
 
 
a=25
console.log(a,typeof a);
a=a.toString();
console.log(a,typeof a);
 
 
//String to number
a="1234"
console.log(a,typeof a);
a=Number(a);
console.log(a,typeof a);
 
a=true;
console.log(a,typeof a);
a=Number(a);
console.log(a,typeof a);
 
a=[1,2,3,4,5];
console.log(a,typeof a);
a=Number(a);
console.log(a,typeof a);
 
 
a="Tutor Joes";
console.log(a,typeof a);
a=Number(a);
console.log(a,typeof a);
 
a='35';
console.log(a,typeof a);
a=parseInt(a);
console.log(a,typeof a);
 
a='35.55';
console.log(a,typeof a);
a=parseInt(a);
console.log(a,typeof a);
 
a='35.55';
console.log(a,typeof a);
a=parseFloat(a);
console.log(a,typeof a);
 
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project