



Planted:
Status: decay
A type can be converted in 2 different ways:
const myVar = otherVar as string.Example, coercing a number into a string:
const a = 42;// explicit coercionconst c = String(a);// implicit coercionconst b = a + "";
Coercion is like translating a word from 1 language to another. For example, translating the word apple from English to German. A translator will look at all the words available in the German language. Then, pick 1 that best matches the English word's meaning. In this case, apfel.
The translator is the JavaScript engine, the word is a value and the languages are JavaScript's types.
Imagine the engine needs to coerce a value from a string to a boolean.
The engine looks at the available "words" in the boolean "language".
There are only 2, true or false.
Whichever is selected, a lot of the string's meaning will be lost in translation.
But that is the nature of coercion.
Whenever coercion occurs, 1 or more internal operations, known as abstract operations, are performed. It always results in a primitive.
To explicitly coerce a value to a string, number or boolean, use the built-in native contructors.
The new keyword isn't used (so an object wrapper isn't created).
const a = 1;const b = String(a);const c = "1";const d = Number(c);const e = "1";const f = Boolean(c);
These constructors can coerce any value to a primitive based on the rules of the abstract operations:
ToStringToNumberToBooleanThe ToString abstract operation rules are:
null becomes "null"undefined becomes "undefined"true becomes "true"1 becomes "1". Very small or large numbers become their exponent form:const a = 1.07 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000;const aStr = a.toString();// seven times three digits => 21 digitsconsole.log(aStr) // 1.07e+21
Object values have their own toString() method.
If you implicitly coerce an object, its toString() will automatically be called.
const obj = [1,2];// explicitconsole.log(obj.toString()) // 1,2// implicit// if either operand to '+' is a string, the operation will be string concatenationconsole.log(obj + "") // 1,2
The ToNumber abstract operation rules are:
true becomes 1false becomes 0undefined becomes NaNnull becomes 0"1" becomes 1, "a" becomes NaNTo coerce an Object value:
ToPrimitive() abstract operation will check if the object has a valueOf() method.toString() does, its return value will be usedToNumber rules above.const objA = {valueOf: function(){return 1;}};const objB = {toString: function(){return "1";}};const objC = [4,2];objC.toString = function(){return this.join( "" );};console.log(Number(objA)) // 1console.log(Number(objB)) // 1console.log(Number(objC)) // 42console.log(Number([])) // 0console.log(Number(["abc"])) // NaN
parseInt(..) can be used to get a numeric value out of a string containing non-numeric characters.
It parses left-to-right and stops when a non-numeric value is found.
const a = "42px";console.log(Number(a)); // NaNconsole.log(parseInt(a)); // 42
The ToBoolean abstract operation rules are:
undefined becomes falsenull becomes false+0, -0, and NaN becomes falsefalsetrueBoolean(myVar) or !!myVar can be used.
var myVar = 1;console.log(Boolean(myVar)); // trueconsole.log(!!myVar); // true
Expression operations that are implicitly coerced to a boolean:
if (..) statement.for ( .. ; .. ; .. ) header.while (..) and do..while(..) loop.? : ternary expression.|| and && operators.
When comparing 2 values for equality, commonly used operators are:
=====Loose allows coercion. Strict doesn't.
const a = 42;const b = "42";console.log(a == b); // trueconsole.log(a === b); // false
Have any feedback about this note or just want to comment on the state of the economy?

