Explain typecasting in Javascript?


Typecasting in JavaScript means converting one data type to another data type i.e., the conversion of a string data type to Boolean or the conversion of an integer data type to string data type. The typecasting in JavaScript is also known as type conversion or type coercion.

Types of conversions

The type casting is of two types in JavaScript. They are the implicit type conversions and the explicit type conversions.

Implicit type casting

The implicit type casting is the conversion of data type done due to the internal requirement or automatic conversion by the compiler or the interpreter.

To understand the implicit conversion let us consider the example of the boolean values (primitive).

JavaScript expects a boolean value in a conditional expression. So JavaScript will temporarily convert the value in parentheses to a boolean to evaluate the if expression −

Example

val = 1; if (val) { console.log( 'yes, val exists' ); }

The values 0, -0, '' (empty string), NaN, undefined, and null, evaluate to false and all other values evaluate to true, even empty arrays and objects.

Implicit conversion with == operator

Type conversion is also performed when comparing values using the equal (==) and not equal (!=) operators. So when you compare the number 125 with a string '125' using the equals (==) operator, the expression evaluates to true −

console.log( 125 == '125' );

Type conversion is not performed when using the identical (===) and not identical (!==) operators.

Explicit type casting

The second type casting the explicit type casting is done forcefully by the developer for the sake of a good line of code. In JavaScript the type casting can be done only for strings, numbers and Boolean (object) data types.

Examples of the explicit type casting are methods parseInt(), parseFloat() and toString().

  • The parseInt() function converts its first argument to a string, parses that string, then returns an integer or NaN.

  • The parseFloat() function parses an argument (converting it to a string first if needed) and returns a floating point number.

  • The toString() method returns a string representing the object, i.e., it tries to convert object to string.

Example

Following example demonstrates explicit type casting in JavaScript.

let a = 1.015 console.log(a) console.log(typeof a) console.log(a.toString()) console.log(typeof a.toString())

Typecasting in Strings

In JavaScript the strings are treated as objects. So, here in string type casting anything like a number or a character given will be converted to a string. The String() is used to convert any given value to string.

Syntax

The typecasting for a string can be done by using the following syntax.

String(input)

The String() method will take one parameter, i.e., the input which is to be converted to string.

Example

Following is an example of this function −

input = 25 console.log("The input value with its type is:",input,typeof(input)) console.log("Arithmetic operation before typecasting with its type is:", (10 + input), typeof((10 + input))) sInput = String(input) console.log("After the type casting is done the type is:",sInput,typeof(sInput)) console.log("Arithmetic operation after typecasting with its type is:", (10 + sInput), typeof(10 + sInput))

In the above example, the given input is a number and it does the addition of numbers before type casting is done. But when the given input is converted to string by the String() method, then instead of addition of the input with 10 it is concatenated as one of the operands is a string even the other is a number. This shows that the type conversion is done.

Typecasting in Boolean

To convert the given input to boolean the Boolean() is used which takes the given value as the parameter and converts it into the Boolean form. This method returns the Boolean values ‘true’ or ‘false’ accordingly. It returns true if the input is at least a character, any number except zero or if it an object. It returns false when the given input is an empty string, zero undefined or a null value.

Example

Following is an example of the Boolean Typecasting with Boolean() in JavaScript −

console.log("The cases where the method will return true") console.log("The given input is rw",Boolean('rw')) console.log("The given input is -3.6",Boolean(-3.6)) console.log("The given input is new Date()",Boolean(new Date())) console.log("The cases where the method will return false") console.log("The given input is number 0",Boolean(0) ) console.log("The given input is null value",Boolean(null) ) console.log("The given input is undefined",Boolean(undefined)) console.log("The given input is empty string",Boolean('') )

Numeric Typecasting

To convert the given input to a number the Number() method is used. It takes one parameter i.e., the input which is given to be converted to number. The conversions can be done to float type or integer type.

To convert it in to Integer type, parseInt() method is used. To convert it in to Float type, parseFloat() is used.

Syntax

Number(input)
parseInt(input)
parseFloat(input)

The three methods will take the given input as the parameter and converts respective of the method used.

Example

This example demonstrates Number() typecasting in JavaScript −

console.log("Input is 7.8.9 and after conversion",Number("7.8.9")) console.log("Input is 6.6.6 and after conversion to float is",parseInt("6.6.6")) console.log("Input is 6.6.6 and after conversion to float is",parseFloat("6.6.6"))

Updated on: 26-Aug-2022

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements