Mastering JavaScript Type Conversion: A Comprehensive Guide for Web Developers

Mastering JavaScript Type Conversion: A Comprehensive Guide for Web Developers

JavaScript Type Conversion

Type conversion in JavaScript refers to the process of converting a value from one data type to another. This process is often necessary when working with different types of data or when performing operations that expect a specific data type.

String to Number

Method 1: parseInt()

let string = "2000";
let convertedNumber = parseInt(string);
console.log(typeof convertedNumber); // Output: number

Method 2: Unary Plus +

let string2 = "2300";
let convertedNumber2 = +string2;
console.log(typeof convertedNumber2); // Output: number

Method 3: Number()

let string3 = "4000";
let convertedNumber3 = Number(string3);
console.log(typeof convertedNumber3); // Output: number

Number to String

Method 1: toString()

let number = 123;
let convertedString = number.toString();
console.log(typeof convertedString); // Output: string

Method 2: String()

let convertedString2 = String(number);
console.log(typeof convertedString2); // Output: string

String to Decimal

Method 1: parseFloat()

let stringDecimal = "20.54";
let convertedDecimal = parseFloat(stringDecimal);
console.log(typeof convertedDecimal); // Output: number

Implicit Type Conversion

JavaScript also performs automatic type conversion, known as implicit type conversion or coercion, when operands of different types are involved in an operation.

let number1 = 10;
let stringNumber = "5";

let result = number1 + stringNumber; // JavaScript implicitly converts number1 to a string
console.log(result); // Output: "105"

Truthy and Falsy Values

JavaScript has truthy and falsy values. When using a non-boolean value in a boolean context (e.g., in an if statement), JavaScript performs type coercion to determine the truthiness or falsiness.

let value = "Hello";

if (value) {
  console.log("Truthy"); // Output: Truthy
} else {

Boolean to String/Number

Method 1: toString()

let booleanValue = true;
let convertedStringBoolean = booleanValue.toString();
console.log(typeof convertedStringBoolean); // Output: string

Method 2: Number()

let convertedNumberBoolean = Number(booleanValue);
console.log(typeof convertedNumberBoolean); // Output: number