> For the complete documentation index, see [llms.txt](https://choubey.gitbook.io/javascript-interview-questions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://choubey.gitbook.io/javascript-interview-questions/question-12.md).

# Question 12

### Question

Can you explain how JavaScript handles type coercion and when it occurs?

### Answer

**Type coercion is the automatic conversion of a value from one data type to another.** While often convenient, it can lead to unexpected results if not understood carefully.

Here's a breakdown:

**When Type Coercion Happens:**

1. **Implicit Type Coercion:** This occurs *automatically* in operations where JavaScript determines that converting a value to a different type is necessary for the operation to make sense.
   * \**Arithmetic Operations (+, -, , /):* When you mix numbers and strings, JavaScript will attempt to convert the string to a number first.

     ```javascript
     console.log(5 + "10"); // Output: 15 (String "10" is coerced to number 10)
     ```
   * **Comparison Operators (==, ===, <, >):** JavaScript will often coerce values on either side of a comparison before making the evaluation. This can lead to surprising results if not careful.

     ```javascript
     console.log(5 == "5"); // Output: true (String "5" is coerced to number 5)
     console.log(5 === "5"); // Output: false (Types are different after coercion)
     ```
2. **Explicit Type Coercion:** You can also force type conversion using explicit methods:
   * `Number()`: Converts a value to a number.
   * `String()`: Converts a value to a string.
   * `Boolean()`: Converts a value to a boolean (true/false).
   * `parseInt()`, `parseFloat()`: Parse strings as integers or floating-point numbers.

**Why Type Coercion Exists:**

* **Convenience:** It simplifies many operations by automatically handling type conversions in common scenarios.
* **Duck Typing:** JavaScript emphasizes "duck typing," where the type of an object matters less than its behavior.

**Potential Pitfalls:**

* **Unexpected Results:** Coercion can lead to bugs if you're not aware it's happening and how it affects your code.
* **Loss of Precision:** Converting between number types (e.g., float to integer) can result in data loss.

**Best Practices:**

* Be mindful of where type coercion occurs in your code.
* Use explicit type conversions when you need to guarantee a specific data type.
* Favor strict equality comparisons (`===`) over loose comparisons (`==`) to avoid unexpected coerced results.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://choubey.gitbook.io/javascript-interview-questions/question-12.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
