Type Conversion

Javascript 6 min min read Updated: Mar 09, 2026 Beginner
Type Conversion
Beginner Topic 6 of 15

Type Conversion in JavaScript

In JavaScript, variables can store different types of values such as numbers, strings, or boolean values. Sometimes a program needs to convert one data type into another. This process is known as Type Conversion.

For example, a number may need to be converted into a string for display purposes, or a string may need to be converted into a number for mathematical calculations.

JavaScript supports two main types of type conversion:

  • Implicit Type Conversion (Automatic)
  • Explicit Type Conversion (Manual)

What is Type Conversion?

Type conversion is the process of converting a value from one data type to another. JavaScript performs type conversion automatically in some cases, but developers can also manually convert data types when required.

1. Implicit Type Conversion

Implicit conversion happens automatically when JavaScript converts one data type to another while performing an operation. This is also called Type Coercion.

Example:

javascript let result = "5" + 3; console.log(result);

Output:

output 53

In this example, the number 3 is automatically converted into a string and then concatenated with "5".

Another example:

javascript let result = "10" - 2; console.log(result);

Output:

output 8

Here, JavaScript converts the string "10" into a number and performs subtraction.

2. Explicit Type Conversion

Explicit type conversion occurs when a developer manually converts one data type into another using built-in JavaScript functions.

Convert String to Number

JavaScript provides several functions to convert strings into numbers.

  • Number()
  • parseInt()
  • parseFloat()

Example:

javascript let num = "100"; let convertedNumber = Number(num); console.log(convertedNumber);

Output:

output 100

Using parseInt()

javascript let value = "25"; let numberValue = parseInt(value); console.log(numberValue);

Using parseFloat()

parseFloat() converts a string into a decimal number.

javascript let price = "19.99"; let newPrice = parseFloat(price); console.log(newPrice);

Convert Number to String

Sometimes numbers must be converted into strings, especially when displaying data on a webpage.

javascript let num = 50; let text = String(num); console.log(text);

Convert Boolean to Number

Boolean values can also be converted into numbers.

javascript console.log(Number(true)); console.log(Number(false));

Output:

output 1 0

Checking the Data Type

You can check the type of a variable using the typeof operator.

javascript let value = "123"; console.log(typeof value);

Output:

output string

Why Type Conversion is Important

Type conversion is very useful when working with user input, form data, APIs, or calculations. For example, when users enter numbers in input fields, they are usually received as strings. Developers must convert them into numbers before performing mathematical operations.

Understanding how type conversion works helps developers avoid unexpected results and write more reliable JavaScript programs.

Conclusion

Type conversion allows JavaScript programs to transform data from one type to another. JavaScript supports both automatic conversion (implicit) and manual conversion (explicit).

Developers commonly use functions like Number(), parseInt(), parseFloat(), and String() to control how values are converted.

In the next tutorial, you will learn about JavaScript Control Statements such as if, else, and switch, which help programs make decisions based on conditions.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators