codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

8 / 65 topics
1JavaScript Introduction2JavaScript console.log() & Output3JavaScript Syntax & Statements4JavaScript Comments5JavaScript Variables (var, let, const)6JavaScript Data Types7JavaScript Operators8JavaScript Type Conversion
Tutorials/JavaScript/JavaScript Type Conversion
🌐JavaScript

JavaScript Type Conversion

Updated 2026-05-12
30 min read

JavaScript Type Conversion

In JavaScript, type conversion is the process of converting a value from one data type to another. This can happen either implicitly (automatically by the JavaScript engine) or explicitly (manually by developers). Understanding how and when these conversions occur is crucial for writing robust and predictable code.

Introduction

JavaScript is a dynamically typed language, meaning that variables can hold values of any type, and their types can change over time. This flexibility often leads to implicit type conversion, where JavaScript automatically converts one data type to another in certain contexts. However, explicit type conversion allows developers to control these conversions more precisely, ensuring that operations are performed as expected.

In this tutorial, we'll explore both implicit and explicit type conversion in JavaScript, focusing on the following methods:

  • String(): Converts a value to a string.
  • Number(): Converts a value to a number.
  • Boolean(): Converts a value to a boolean.
  • parseInt(): Parses a string and returns an integer.

Implicit Type Conversion

Implicit type conversion occurs when JavaScript automatically converts one data type to another. This often happens in operations involving different types, such as arithmetic or concatenation.

Arithmetic Operations

When you perform arithmetic operations with different types, JavaScript will attempt to convert the values to numbers.

script.js
1let num = 5;
2let str = "10";
3console.log(num + str); // Output: "510"
4console.log(num - str); // Output: -5
Output
510
-5

In the first example, num + str results in string concatenation because adding a number to a string converts the number to a string. In the second example, num - str triggers numeric subtraction because subtracting a string from a number converts the string to a number.

Equality Comparison

Equality comparison (==) also involves implicit type conversion. JavaScript will attempt to convert both operands to the same type before making the comparison.

script.js
1console.log(5 == "5"); // Output: true
2console.log(0 == false); // Output: true
Output
true
true

In both cases, JavaScript converts the operands to a common type before comparing them. This can lead to unexpected results if not handled carefully.

Explicit Type Conversion

Explicit type conversion is performed using built-in functions that allow developers to control how values are converted from one type to another.

String Conversion

The String() function converts any value to a string.

script.js
1let num = 123;
2let bool = true;
3console.log(String(num)); // Output: "123"
4console.log(String(bool)); // Output: "true"
Output
"123"
"true"

The String() function is straightforward and handles most types, including numbers, booleans, and objects.

Number Conversion

The Number() function converts a value to a number. If the conversion fails, it returns NaN.

script.js
1let str = "123";
2let bool = true;
3console.log(Number(str)); // Output: 123
4console.log(Number(bool)); // Output: 1 (true) or 0 (false)
Output
123
1

The Number() function is useful for converting strings to numbers, but be cautious with non-numeric strings, as they will result in NaN.

Boolean Conversion

The Boolean() function converts a value to a boolean. In JavaScript, the following values are considered "falsy":

  • false
  • 0
  • "" (empty string)
  • null
  • undefined
  • NaN

All other values are considered "truthy."

script.js
1let num = 0;
2let str = "";
3console.log(Boolean(num)); // Output: false
4console.log(Boolean(str)); // Output: false
Output
false
false

The Boolean() function is useful for determining the truthiness of a value, especially in conditional statements.

Parsing Integers

The parseInt() function parses a string and returns an integer. It stops parsing when it encounters a non-numeric character.

script.js
1let str = "123abc";
2console.log(parseInt(str)); // Output: 123
Output
123

The parseInt() function is particularly useful for extracting numbers from strings that contain non-numeric characters.

Practical Example

Let's create a simple program that demonstrates both implicit and explicit type conversion. The program will take user input, convert it to a number, perform some arithmetic operations, and display the results.

script.js
1// Function to get user input
2function getUserInput() {
3return prompt("Enter a number:");
4}
5
6// Convert user input to a number
7let userInput = getUserInput();
8let num = Number(userInput);
9
10// Perform arithmetic operations
11let double = num * 2;
12let half = num / 2;
13
14// Display results
15console.log(`You entered: ${num}`);
16console.log(`Double of your number is: ${double}`);
17console.log(`Half of your number is: ${half}`);

This program uses the prompt() function to get user input, converts it to a number using Number(), and then performs arithmetic operations. The results are displayed in the console.

Summary

Conversion TypeMethodDescription
StringString(value)Converts any value to a string.
NumberNumber(value)Converts a value to a number. Returns NaN for non-numeric strings.
BooleanBoolean(value)Converts a value to a boolean. Uses truthy/falsy rules.
Integer ParsingparseInt(string)Parses a string and returns an integer. Stops at non-numeric characters.

What's Next?

In the next tutorial, we'll explore conditional statements in JavaScript using the if...else construct. This will allow us to execute different blocks of code based on certain conditions, building on our understanding of type conversion.

Stay tuned for more advanced topics and deepen your JavaScript skills!


PreviousJavaScript OperatorsNext JavaScript if...else Statement

Recommended Gear

JavaScript OperatorsJavaScript if...else Statement