---
title: "What is type conversion in JavaScript? Explain with examples."  
description: "What is type conversion in JavaScript? Explain with examples."  
author: "Steilla Mitchel"  
published: 2023-10-30  
updated: 2023-10-31  
canonical: https://www.mindstick.com/forum/160324/what-is-type-conversion-in-javascript-explain-with-examples  
category: "javascript"  
tags: ["javascript", "type conversion"]  
reading_time: 2 minutes  

---

# What is type conversion in JavaScript? Explain with examples.

What is type [conversion](https://www.mindstick.com/forum/1734/conversion-from-32-bit-integer-to-4-chars) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)? [Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with examples.

## Replies

### Reply by Aryan Kumar

Type conversion, also known as type coercion, is the process in JavaScript where a value of one data type is automatically or explicitly converted to another data type. JavaScript is a dynamically typed language, which means that variables can hold values of different types, and type conversion is a common practice to ensure that values are in the expected format for various operations.

There are two types of type conversion in JavaScript:

**Implicit Type Conversion (Coercion):** This occurs automatically by the JavaScript engine when values of different types are used together in an operation. JavaScript tries to convert one or both of the values to a common data type before performing the operation.

Example 1: Implicit conversion of a number to a string.

```plaintext
let number = 42;
let string = "The answer is: " + number; // The number is implicitly converted to a string.
```

Example 2: Implicit conversion of a string to a number (if possible).

```plaintext
let strNum = "42";
let num = strNum * 1; // The string "42" is implicitly converted to a number.
```

**Explicit Type Conversion:** This type of conversion is done explicitly in your code using functions like **Number()**, **String()**, or **Boolean()** to change a value from one type to another.

Example 1: Explicit conversion from a string to a number.

```plaintext
let str = "123";
let num = Number(str); // Explicitly converts the string "123" to a number.
```

Example 2: Explicit conversion from a number to a string.

```plaintext
let number = 42;
let str = String(number); // Explicitly converts the number 42 to a string.
```

Example 3: Explicit conversion from a value to a boolean.

```plaintext
let value = "Hello";
let bool = Boolean(value); // Explicitly converts the value to a boolean (true, since it's not a falsy value).
```

Type conversion is a fundamental aspect of JavaScript, and understanding how it works is crucial for writing code that behaves as expected. Implicit type conversion can sometimes lead to unexpected results, so it's important to be aware of the rules governing such conversions and use explicit type conversion when needed to ensure your code's correctness.


---

Original Source: https://www.mindstick.com/forum/160324/what-is-type-conversion-in-javascript-explain-with-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
