JavaScript Type Conversion

In JavaScript, type conversion refers to changing a value from one type to another. It can happen automatically (implicit) or manually (explicit). Understanding how JavaScript handles type conversion helps avoid unexpected bugs in your code.



What You'll Learn

You'll learn how JavaScript converts types behind the scenes (implicitly) and how to do it yourself using built-in functions (explicitly). We'll cover conversion between strings, numbers, and booleans.


Implicit Type Conversion

In JavaScript, when you mix different types (like a string and a number), JavaScript automatically changes one type to match the other. This is called "implicit type conversion" or "type coercion".

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

How It Works:

  • "5": This is a string value.
  • 3: This is a number.
  • When using +, JavaScript sees a string and a number, so it changes the number to a string.
  • Then it combines them as strings: "5" + "3" becomes "53".
  • console.log() prints the result to the screen.

Output

53

Explicit Type Conversion

In JavaScript, you can manually convert one data type to another using built-in functions like Number(), String(), and Boolean(). This is called "explicit type conversion".

javascript
let str = "123";
let num = Number(str);
console.log(num);

How It Works:

  • "123": This is a string that looks like a number.
  • Number(str): This function converts the string into an actual number.
  • num: The variable now holds the number 123 (not a string).
  • console.log(): This prints the numeric value to the screen.

Output

123

Example 1: String to Number

Convert a string that looks like a number into an actual number using Number().

javascript
let str = "45";
let num = Number(str);
console.log(num + " is of type " + typeof num);

How It Works:

  • "45" is a string.
  • Number(str) converts it to the number 45.
  • console.log(num) prints the number.

Output

45 is of type number

Example 2: Number to String

Convert a number to a string using String().

javascript
let age = 30;
let strAge = String(age);
console.log(strAge + " is of type " + typeof strAge);

How It Works:

  • 30 is a number.
  • String(age) turns it into the string "30".
  • console.log(strAge) prints the string.

Output

30 is of type string

Example 3: Any Value to Boolean

Convert different types to boolean using Boolean().

javascript
let isEmpty = Boolean("");
let isFilled = Boolean("hello");

console.log(isEmpty + " is the boolean value of an empty string");
console.log(isFilled + " is the boolean value of a non-empty string");

How It Works:

  • "" is an empty string, so Boolean("") gives false.
  • "hello" is a non-empty string, so Boolean("hello") gives true.

Output

false is the boolean value of an empty string
true is the boolean value of a non-empty string

Example 4: Boolean to Number

Convert boolean values to numbers using Number(). true becomes 1, and false becomes 0.

javascript
let yes = Number(true);
let no = Number(false);

console.log(yes + " is the numeric value of true");
console.log(no + " is the numeric value of false");

How It Works:

  • true becomes 1.
  • false becomes 0.

Output

1 is the numeric value of true  
0 is the numeric value of false

Example 5: Invalid String to Number

If a string doesn’t represent a valid number, using Number() will return NaN (Not a Number).

javascript
let invalid = Number("hello");
console.log(invalid + " is the result of trying to convert 'hello' to a number");

How It Works:

  • "hello" is not a valid number.
  • Number("hello") returns NaN.
  • NaN stands for "Not a Number".

Output

NaN is the result of trying to convert 'hello' to a number

Exercises

Practice type conversion with these simple exercises:

1. Convert a string number to a real number and print it.
javascript
let strNum = "99";
let realNum = Number(strNum);
console.log(realNum); // 99

2. Convert a number to a string and print it.
javascript
let num = 123;
let str = String(num);
console.log(str); // "123"

3. Convert a value to a boolean and print it.
javascript
let empty = "";
let isEmpty = Boolean(empty);
console.log(isEmpty); // false

*Try experimenting with null, undefined, and other values.


Frequently Asked Questions

What is type conversion in JavaScript?

Type conversion in JavaScript refers to the process of converting one data type to another, such as converting a string to a number or a boolean to a string. This can happen implicitly or explicitly.


What is implicit type conversion?

Implicit type conversion (or coercion) occurs when JavaScript automatically converts one data type to another, usually during operations involving different types, like adding a number to a string.


What is explicit type conversion?

Explicit type conversion happens when you manually convert a value from one type to another using methods such as Number(), String(), or Boolean().


How do I convert a string to a number in JavaScript?

You can convert a string to a number in JavaScript using the Number() function or by using the unary plus (+) operator.


How do I convert a number to a string in JavaScript?

To convert a number to a string in JavaScript, you can use the String() function or the .toString() method.



What's Next?

Next, you'll dive into the JavaScript `if-else` statement and learn how to control the flow of your program based on conditions.