Type Coercion in JavaScript

Understanding Type Coercion in JavaScript: Basics and Beyond

techiydude
7 min readNov 9, 2024

--

Automatic type conversion, also known as type coercion, is a way of converting one data type to another without explicit instructions. This occurs in various contexts, such as comparisons, arithmetic operations, and even when values are assigned to variables. JavaScript’s coercion falls into two categories:

Implicit Coercion: JavaScript converts types automatically.

Explicit Coercion: Developers manually convert types using functions like Number(), String(), and Boolean().

🔍 What is Happening Behind the Scenes? The Basics of Type Coercion

JavaScript is a dynamically typed language, which means it does not need explicit data types. Rather, it attempts to automatically transform one data type into another, especially during operations that involve various types. This procedure, referred to as type coercion, can occur in both implicit (automatically executed) and explicit (manually executed) forms.

For example

console.log('5' + 3); // "53"
console.log('5' - 3); // 2

Notice how in the first case, JavaScript concatenates, while in the second, it subtracts. JavaScript’s rules for…

--

--

techiydude
techiydude

Written by techiydude

I’m a developer who shares advanced Laravel and Node.js insights on Medium.

Responses (6)