What is Type Casting in Javascript
This article is about I would like to discuss what is type casting in Javascript.
What is type casting?
Converting one type of data value to another type is called typecasting. While developing an application, you may come across some situations where you need to convert one type of value to another type to get the desired results. There are two types of typecasting they are:
- Implicit type casting (Coercion): Done by JavaScript Engine.
- Explicit type casting (Conversion): Done by programmers.
Implicit type casting
If required JavaScript engine automatically converts one type of value to another type. This is known as implicit type casting.
Ex:
document.write(2+4.3); //6.3
document.write("2"+2); //"22"
document.write(2+"2"); //"22"
In the above code, For the first line, we get the value 63 as output. First, we have given to add integer and float values. The browser first adds “.0” to the integer value and converts it to float,for ex: “int=6 > 6.0”. Then add the two float values, So we get the output “6.3”. Implicitly the browser converts the integer number to a real number before evaluating the expression, So which is called as implicit typecasting.
In the second line, we have given to add string and integer value, we get the value 22 as output. The browser observes the type of values that we are passing to the plus (+) operator. It implicitly converts the number (2) to string (2) then it evaluates the expression.
Explicit type casting
If required programmers can also convert one type of value to another type. This is known as explicit type casting.
For explicit type casting, we use the built-in function:
- parseInt() function
- parseFloat() function
ParseInt()
It converts a given value to an integer number, if not possible to convert then returns the “NaN” value, which means “not a number”.
Ex:
If the given value is of integer type, then it returns the number as it is.
document.write(parseInt(24)); //24
document.write(parseInt(13+13)); //26
If the given value is of float type, then it discards the floating part and returns the integer part of the number.
Ex:
document.write(parseInt(4.143)); //4
document.write(parseInt(2.3+3.3)); //5
If the given value is of string type, then it tries to extract and return the beginning “integer” part.
If the string passed to the parseInt function does not begin with an integer then it returns the NaN value.
Ex:
document.write(parseInt("24")); //24
document.write(parseInt("25th text")); //25
document.write(parseInt("2"+"6")); //26
document.write(parseInt("2a"+"6a")); //2
document.write(parseInt("sometext25")); //NaN
parseFloat function
It converts a given value to a float number, if not possible to convert then returns the Nan value, which means “not a number”.
Ex:
If the given value is of integer type, then it converts and returns an equivalent float number.
document.write(parseFloat(25)); // 25
document.write(parseFloat(25+25)); // 50
If the given value is of float type, then it returns the float number as it is.
Ex:
document.write(parseFloat(25.234)); // 25.234
document.write(parseFloat(2.5+2.7)); //5.2
If the given value is of string type, then it tries to extract and return the beginning “float” part.
If the string passed to the “parseFloat” function does not begin with a float number, then it returns the NaN value.
Ex:
document.write(parseFloat(".24")); //0.24
document.write(parseFloat("3.142.142")); //3.142
document.write(parseFloat("3.142sometext")); //3.142
document.write(parseFloat("sometext3.145")); //NaN
document.write(parseFloat("2"+"7")); // 9