Type coercion (Type Casting) in JavaScript || Hindi || Coding Scenes

Опубликовано: 04 Июль 2026
на канале: Coding Scenes
801
20

Type coercion is the automatic or implicit conversion of values from one data type to another. For example, converting a string value to an equivalent number value. It is also known as type conversion.
================================
Implicit Coercion:
10 + "" //Output is "10" as number 10 is converted to string "10"
"10" * 2 //Output is 20 as string 10 is converted to number 10
"15" - "11" //Output is 4 as both the strings are converted to number
undefined + 6 //Output is NaN as undefined could not be converted to number
"Goodnight" + null //Output is "Goodnightnull" as null is converted to string "null"
null + 25 //Output is 25 as null is converted to 0.
true + true //Output is 2 as true is converted to number 1.
false + 10 //Output is 10 as false is converted to number 0.
10 * [6] //Output is 60 as [6] is converted to number 6.
==================================
Explicit Coercion:
String("true") // "true"
String({name: "Rohit"}) // String() method will stringify the object with '[object Object]'
Number("20") //Output is 20 as "20" string is converted to number 20
Number("") //Output is 0 as "" string is converted to 0
Number([]) //Output is 0 as [] is converted to 0
Number(null) //Output is 0 as null is converted to 0
Number(true) //Output is 1 as true is converted to 1
Number(false) //Output is 0 as false is converted to 0
Number("Test") //Output is NaN as "Test" could not be converted to number