The data types in JS are: Object Null Undefined number String Boolean
Convert to Boolean type:boolean ();
Conversion rules:
Data type |
Value converted to True |
Value converted to False |
Boolean |
True |
False |
String |
Any non-empty string |
"" (empty string) |
Number |
Any non-0 numeric value |
0 and Nan |
Undefined |
Any object |
Null |
Object |
No |
Undefined |
Convert to String type:string () and toString ();
conversion rule:
<script> var a= "1"; var b= "true"; var c= "null"; var D; Alert (String (a)); // "1" alert (String (b)); // "true" Alert (String (c)); // "NULL" alert (String (d)); // "undefined"</script>
ToString (); method is not able to convert Null and Undefined
Convert to Floating point:parsefloat ();
<script> var num1 = parsefloat ("1234red") //1234 var num2 = Parsefloat ("22.5") //22.5 var num3 = parsefloat ("22.34.5") // 22.34 var num4 = parsefloat ("0908.4") //908.4</ Script>
Convert to Integer:parseint ();
<script> var num1 = parseint ("1234red") //1234 var num2 = parseint ("") //NaN var num3 = parseint ("22.5") // var num4 = parseint ("a") //</script>
Non-numeric conversion to numeric number ();
<script> var a= "1"; Alert (number (a)); var num1 = number ("Hello world"); // NaN var num2 = number (""); // 0 var num3 = number ("0000011"); // One var num4 = number ("true"); // 1</script>
The conversion of each data type in JS