Type Casting - Part 2

Notes:

JavaScript 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 built in functions:
parseInt function
parseFloat function
eval function

parseInt(value):
Converts a given value to an integer number, if not possible to convert then returns NaN value
Ex:
If the given value is of integer type, then it returns number as it is.
document.write( parseInt(24) ); // 24
document.write(parseInt(12+12)); // 24

If the given value is of float type, then it discards the float part and returns integer part of the number
document.write(parseInt(3.142)); // 3
document.write(parseInt(1.6+1.6)); // 3

If the given value is of string type, then it tries to extract and return the beginning integer part.
If string passed to the parseInt function does not begin with integer then it returns NaN value
document.write(parseInt(“24”)); // 24
document.write(parseInt(“3.142”)); // 3
document.write(parseInt(“24sometext”)); // 24
document.write(parseInt(“2”+”4”)); // 24
document.write(parseInt(“2”+”4a”)); // 24
document.write(parseInt(“2a”+”4a”)); // 2
document.write(parseInt(“sometext24”)); // NaN

Interview Questions: