- Home
- javascript Tutorial
- type-casting
Type Casting
Converting a data type into another data type is known as typecasting.
This is used when there is a need to convert the value of one data type into the value of another data type.
The typeof Operator:
The typeof operator is used to find the type of a javascript variable. The typeof operator returns the type of a variable or an expression.
Example:
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof 0 + "<br>" +
typeof "john" + "<br>" +
typeof "" + "<br>" +
typeof (3);
</script>
</body>
</html>
Output:
string
string
number
Converting to Boolean
For converting a value to boolean data type it needs to be passed as a parameter into the Boolean function.
Syntax:
Example
var b = 1;
b = Boolean(b);
document.write(b + " : " + typeof b); //outputs true : boolean
</script>
Converting to String
For converting a value to string data type it needs to be passed as a parameter into the String function.
Syntax:
Example
var s = 1;
s= String(s);
document.write(s + " : " + typeof s); //outputs 1 : string
</script>
Converting to Number
For converting a value to string data type it needs to be passed as a parameter into the String function.
Syntax:
Example
var n1 = true;
var n2 = "1str";
var n3 = "123";
n1 = Number(n1);
n2 = Number(n2);
n3 = Number(n3);
document.write(n1 + " : " + typeof n1); //outputs 1 : number
document.write("<br />");
document.write(n2 + " : " + typeof n2); //outputs NaN : number
document.write("<br />");
document.write(n3 + " : " + typeof n3); //outputs 123 : number
document.write("<br />");
</script>
true is represented as 1 in numeric so n1 after conversion stores value 1. "1str" after conversion doesn't represent a valid number that's why the output is NaN. NaN means not a number. Its a language construct. "123" after conversion represents valid number 123.
ParseInt
ParseInt() function is used for converting string values into numbers. ParseInt is used in converting only strings values that contain numbers unlike Number() which can be used to typecast string as well as boolean values into number type. For all the invalid types given as input parseInt() will return NaN.
Syntax:
Example
var n1 = true;
var n2 = "1str";
var n3 = "123";
var n4 = "str123str1";
n1 = parseInt(n1);
n2 = parseInt(n2);
n3 = parseInt(n3);
n4 = parseInt(n4);
document.write(n1 + " : " + typeof n1); //outputs NaN : number
document.write("<br />");
document.write(n2 + " : " + typeof n2); //outputs 1: number
document.write("<br />");
document.write(n3 + " : " + typeof n3); //outputs 123 : number
document.write("<br />");
document.write(n3 + " : " + typeof n4); //outputs 123 : number
document.write("<br />");
</script>
parseFloat
parseFloat() function is used for converting strings values into floating point numbers. It works similar to parseInt() function with an exception of handling decimal point numbers. For example, 1.23 is represented as 123e-2. parseInt("123e-2") will result in 123 but parseFloat("123e-2") will give 1.23.
Syntax:
Example
var n1 = "123e-2";
var n2 = "1str";
var n3 = "123";
var n4 = "str123str1";
n1 = parseFloat(n1);
n2 = parseFloat(n2);
n3 = parseFloat(n3);
n4 = parseFloat(n4);
document.write(n1 + " : " + typeof n1); //outputs 1.23 : number
document.write("<br />");
document.write(n2 + " : " + typeof n2); //outputs 1: number
document.write("<br />");
document.write(n3 + " : " + typeof n3); //outputs 123 : number
document.write("<br />");
document.write(n3 + " : " + typeof n4); //outputs 123 : number
document.write("<br />");
</script>