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

Published: 19 July 2022
on channel: 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


On this page of the site you can watch the video online Type coercion (Type Casting) in JavaScript || Hindi || Coding Scenes with a duration of hours minute second in good quality, which was uploaded by the user Coding Scenes 19 July 2022, share the link with friends and acquaintances, this video has already been watched 801 times on youtube and it was liked by 20 viewers. Enjoy your viewing!