DEV Community

vidhya murali
vidhya murali

Posted on

Type Conversion & Type Coercion in JavaScript

JavaScript Type Conversion

In JavaScript Type Conversion can be defined as converting the data type of the variables from one type to the other manually by the programmer(explicitly) or automatically by the JavaScript(implicitly).

  • Implicit Type Conversion (Coercion): Implicit Type Conversion occurs automatically by the JavaScript.
  • Explicit Type Conversion: Explicit Type Conversion occurs when the programmer manually changes the type of the variables using the function Number(), String(), and Boolean().

Implicit Type Conversion (Coercion)
In JavaScript, the implicit type conversion or coercion conversion can be defined as the automatic conversion of the data type of the variables from one type to another type. Implicit type conversion mostly occurs when we are performing the arithmetic or the logical operations.

  • Coercion is implicit (handled automatically by the engine), while type conversion is explicit (you manually use functions like Number() or String()).
  • Because JavaScript is weakly typed, it doesn't throw errors when types don't match; it just tries to coerce them.

[Type coercion happens when you perform an operation on different data types, and the JavaScript engine tries to make them "fit" together.]

String with Number (Concatenation)

When we add a string with the number, the JavaScript automatically converts the number into a string and performs string concatenation.

let res= "15"+5;
console.log(res); [The output will be 20.]

Boolean to Number

  • When we perform the mathematical operations, then JavaScript automatically converts true to 1 and false to 0.
  • Values are coerced to booleans in logical contexts like if statements or using the double-NOT operator (!!).
  • Falsy values: 0, "", null, undefined, NaN, and false.
  • Truthy values: Everything else (including empty objects {} and arrays []).

Equality Comparison (==)

When we use the equality operator in the JavaScript, it compares them after converting the value into the same data type.
[Use === (Strict Equality): This checks both value and type without coercion, preventing weird results.]

Automatic Conversion in Logical Operations

In JavaScript, these values are automatically converts undefined, null, "" (empty string), false, NaN, and 0 to false, and all other values to true.

Explicit Type Conversion

In JavaScript, explicit type conversion can be defined as when we manually change the data type of the variable from one to other using some built-in JavaScript functions. JavaScript provides us the built-in functions for performing the explicit conversion.

Converting Strings to Numbers

The global method Number() converts a variable (or a value) into a number.

  • A numeric string (like "3.14") converts to a number (like 3.14).
  • An empty string (like "") converts to 0.
  • A non numeric string (like "John") converts to NaN (Not a Number).

Converting Numbers to Strings

  • The global method String() can convert numbers to strings. It can be used on any type of numbers, literals, variables, or expressions:

Converting to Boolean

In JavaScript, we can convert the value into a boolean we can use the Boolean() function

Reasons to Learn JavaScript Type Conversion

  • Data Handling: In JavaScript when we are working with API responses, user inputs and calculations, these type of the operation require type conversion.
  • Prevent Bugs: It is important to understand the type conversion which can help in preventing bugs which occurs when JavaScript implicitly converts value.
  • Code Clarity: By performing the explicit conversion makes our code bug free and more clear.

Top comments (0)