JavaScript converts values when an operation combines different types. This type coercion can be useful, but the conversion depends on the operator: values that compare equal may still produce an unexpected result when added. section 3.3.2
The same prices, two different operations
In `primer/index.js`, `100 == "100"` is true because abstract equality converts the string to a number. But `100 + "100"` produces the string `"100100"`: when either operand of `+` is a string, JavaScript concatenates. Use `===` to compare without that equality conversion, and `Number(bootsPrice)` when the intended operation is numeric addition. section 3.3.2
Listing 3.10 Preventing coercion in the index.js file in the primer folder
let hatPrice = 100;
console.log(`Hat price: ${hatPrice}`);
let bootsPrice = "100";
console.log(`Boots price: ${bootsPrice}`);
if (hatPrice === bootsPrice) {
console.log("Prices are the same");
} else {
console.log("Prices are different");
}
let totalPrice = Number(hatPrice) + Number(bootsPrice);
console.log(`Total Price: ${totalPrice}`);
let myVariable = "Adam";
console.log(`Type: ${typeof myVariable}`);
myVariable = 100;
console.log(`Type: ${typeof myVariable}`);
The `index.js` listing puts both choices together: strict equality reports different types as different prices, while explicit conversion produces a numeric total of `200`. section 3.3.2
Conversion also affects function results. A function that adds its arguments can return a string when one argument is a string. If an argument is omitted, its parameter is `undefined`: string concatenation can include the word `undefined`, while numeric addition can produce `NaN`. A default parameter handles an omitted value, but does not turn a supplied string into a number. section 3.3.3
Listing 3.18 Converting and filtering parameters in the index.js file in the primer folder
let hatPrice = 100;
console.log(`Hat price: ${hatPrice}`);
let bootsPrice = "100";
console.log(`Boots price: ${bootsPrice}`);
function sumPrices(...numbers) {
return numbers.reduce(function(total, val) {
return total + (Number.isNaN(Number(val)) ? 0 : Number(val));
}, 0);
}
let totalPrice = sumPrices(hatPrice, bootsPrice);
console.log(`Total: ${totalPrice} ${typeof totalPrice}`);
totalPrice = sumPrices(100, 200, 300);
console.log(`Total: ${totalPrice} ${typeof totalPrice}`);
totalPrice = sumPrices(100, 200, undefined, false, "hello");
console.log(`Total: ${totalPrice} ${typeof totalPrice}`);
This `index.js` version accepts a variable number of values, converts each with `Number`, and substitutes zero when conversion yields `NaN`. It returns numeric totals for the calls shown, including one containing `undefined`, `false`, and `"hello"`. section 3.3.3
Fallback expressions need a separate decision. `taxRate || 10` replaces an intended zero tax rate with `10`; `taxRate ?? 10` uses the fallback for `null` or `undefined` while preserving zero. The same nullish fallback can supply a value for a missing object property, alongside optional chaining when the object itself may be absent. section 3.3.2 section 3.5.1
Listing 7.13 Changing the result type in the index.ts file in the src folder
function calculateTax(amount: number) {
return (amount * 1.2).toFixed(2);
}
let price = 100;
let taxAmount = calculateTax(price);
let halfShare = taxAmount / 2;
console.log(`Full amount in tax: ${taxAmount}`);
console.log(`Half share: ${halfShare}`);
In `src/index.ts`, `toFixed(2)` makes `calculateTax` return a string. JavaScript could convert that string for division, but TypeScript reports an arithmetic type error instead; convert deliberately rather than relying on that coercion. section 7.2.2




















