Type Conversion

Type conversion, also known as type casting, is the explicit conversion of a value from one data type to another in a programming language. Unlike type coercion, which is implicit and happens automatically, type conversion requires an explicit conversion operation in the code.

Type conversion is necessary when performing operations that require operands to be of a specific type or when converting data for compatibility with certain functions or operations. Most programming languages provide built-in functions or syntax for performing type conversion.

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

// String to Number
let str = '123';
let num = Number(str);
console.log(num); // Output: 123

// Number to String
let num = 123;
let str = String(num);
console.log(str); // Output: '123'

// Boolean to Number
let bool = true;
let num = Number(bool);
console.log(num); // Output: 1

// Number to Boolean
let num = 0;
let bool = Boolean(num);
console.log(bool); // Output: false

Explanation of the Examples: