JAVASCRIPT

JavaScript Type Conversion: Syntax, Usage, and Examples

JavaScript type conversion changes a value from one data type to another, either implicitly (automatic conversion by JavaScript) or explicitly (manually converting a value using built-in functions). Understanding how type conversion works helps prevent unexpected behavior in JavaScript programs.

How Type Conversion Works in JavaScript

JavaScript automatically converts values when needed, a process known as implicit type conversion (or type coercion). When you manually change a value’s type, it’s called explicit type conversion (or type casting).

Implicit Type Conversion

JavaScript converts types automatically when performing operations between different data types.

console.log("5" + 2);  // "52" (number converted to a string)
console.log("5" - 2);  // 3 (string converted to a number)
console.log(true + 1); // 2 (boolean converted to a number)

In the first case, JavaScript treats 5 as a string and concatenates it with 2. In the second case, the subtraction operator forces "5" to be converted into a number before performing the calculation.

Explicit Type Conversion

You can use JavaScript's built-in functions to convert values manually.

let num = Number("42"); // Converts a string to a number
let str = String(42);   // Converts a number to a string
let bool = Boolean(1);  // Converts 1 to true

These functions let you control type conversion rather than relying on JavaScript’s implicit behavior.

When to Use Type Conversion in JavaScript

Type conversion is useful when:

  1. You need to ensure user input is treated as a number rather than a string.
  2. You are comparing values and want to avoid unexpected coercion.
  3. You want to explicitly convert values to avoid JavaScript’s automatic type coercion.

Examples of Type Conversion in JavaScript

Converting Strings to Numbers

Use Number(), parseInt(), or parseFloat() to convert strings into numbers.

let num1 = Number("42");    // 42
let num2 = parseInt("42px"); // 42 (ignores non-numeric characters)
let num3 = parseFloat("3.14"); // 3.14

If the string doesn’t contain a valid number, Number() returns NaN (Not-a-Number).

Converting Numbers to Strings

Use String() or .toString() to convert numbers into strings.

let str1 = String(42);    // "42"
let str2 = (42).toString(); // "42"

This is useful when displaying numbers in user interfaces.

Boolean Conversion

Values automatically convert to true or false when used in boolean contexts, but you can force the conversion using Boolean().

console.log(Boolean(0));     // false
console.log(Boolean(1));     // true
console.log(Boolean(""));    // false (empty string)
console.log(Boolean("Hi"));  // true (non-empty string)
console.log(Boolean(null));  // false

JavaScript treats empty values (0, "", null, undefined, and NaN) as false, while everything else evaluates to true.

Type Conversion in Comparisons

JavaScript sometimes converts types automatically when comparing values.

console.log(5 == "5");  // true (string converted to number)
console.log(5 === "5"); // false (strict comparison, no conversion)

The == operator allows type conversion, while === checks both value and type. To avoid unexpected results, prefer === for comparisons.

Learn More About Type Conversion in JavaScript

Automatic Type Conversion in Arithmetic

JavaScript automatically converts types when using mathematical operators.

console.log("5" * 2);   // 10 (string converted to number)
console.log("10" / "2"); // 5 (both strings converted to numbers)
console.log("5" + 2);   // "52" (string concatenation)

The + operator prioritizes string concatenation, while other arithmetic operations trigger number conversion.

Type Conversion in JavaScript Conditionals

When using conditions like if statements, JavaScript converts values into booleans.

if ("hello") {
  console.log("This runs!"); // Runs because non-empty strings are truthy
}

if (0) {
  console.log("This won't run."); // Doesn't run because 0 is falsy
}

Understanding truthy and falsy values helps you write cleaner conditional logic.

Converting Objects to Primitives

JavaScript automatically converts objects to primitive values when needed.

let obj = { valueOf: () => 42 };
console.log(obj + 10); // 52 (object converted to number)

Objects use .toString() and .valueOf() for conversion.

Type Conversion in JavaScript Data Types

Different data types behave uniquely when converted.

console.log(Number(null));  // 0
console.log(Number(undefined)); // NaN
console.log(String([1, 2, 3])); // "1,2,3"
console.log(Boolean([])); // true (empty arrays are truthy)

null converts to 0, while undefined results in NaN. Arrays convert to strings by joining their elements with commas.

JavaScript type conversion happens automatically in many cases, but you can control it using built-in functions like Number(), String(), and Boolean().

Learn to Code in JavaScript for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH