Type Coercion

Type coercion is the automatic or implicit conversion of values from one data type to another in a programming language. This conversion can occur when an operator or function requires its arguments to be of a different type than the ones provided.

In languages like JavaScript, type coercion can happen in various scenarios, such as when performing arithmetic operations on different types, comparing values of different types using equality operators (==), or using values in a boolean context (e.g., in an if statement).

Example: Here are some examples of type coercion in JavaScript:

// Addition
console.log(5 + '5'); // Output: '55' (number is coerced to a string)

// Comparison
console.log(5 == '5'); // Output: true (string is coerced to a number)

// Boolean context
console.log(!!'hello'); // Output: true (string is coerced to a boolean)

Explanation of the Examples: