Type Coercion refers to the process of automatic or implicit conversion of values from one data type to another. This includes conversion from Number to String, String to Number, Boolean to Number etc. when different types of operators are applied to the values.
In case the behavior of the implicit conversion is not sure, the constructors of a data type can be used to convert any value to that datatype, like the Number(), String() or Boolean() constructor.
The Number 10 is converted to string '10' and then '+' concatenates both strings
var x = 10 + "20";
var y = "20" + 10;
var z = true + "10";
console.log(x, y, z); //1020 2010 true10
var w = 10 - "5";
var x = 10 * "5";
var y = 10 / "5";
var z = 10 % "5";
console.log(w, x, y, z); //5 50 2 0
var x = true + 2;
var y = false + 2;
console.log(x, y); // 3 2
var x = 10 == "10";
var y = true == 1;
var z = true == "true";
console.log(x, y, z); //true true false
let ans = String(10);
console.log(typeof ans); //string
ans = Number("10");
console.log(ans); //number
ans = Number("learnjavascript");
console.log(ans); //NaN
ans = Boolean(10);
console.log(ans); //true
ans = Boolean(0);
console.log(ans); //false
implicit type conversion /automatic type conversion
- for example number se string me conversion string se number me convrsion
let x = "20";
console.log(x / 2);
let x = "20";
console.log(Number(x) + 20); // //string to number
let y = 10;
console.log(string(y) + 200); //number to string