Dynamic typing lets values carry their types at runtime, so one variable can refer to a string, number, or boolean at different points. JavaScript infers each type from the assigned value. section 3.3.1 section 7.2
Expect these behaviors when working with dynamically typed JavaScript:
- Inspect a value with typeof; assigning a new value can change the reported type. section 3.3.1 section 7.2
- Leave a variable unassigned and its value and type are undefined until a value is assigned. section 7.2
- Rely on JavaScript coercion carefully: loose equality can equate a number and string, while addition can concatenate them. section 3.3.1
- Remember that typeof null returns object, a long-standing JavaScript behavior. section 3.3.1
A flexible parameter can hide an incorrect assumption
function calculateTax(amount) { return amount * 1.2; } console.log(calculateTax(12)); // 14.399999999999999 console.log(calculateTax("Hello")); // NaN console.log(calculateTax(true)); // 1.2 The function accepts number, string, and boolean arguments. JavaScript processes each call, but only the numeric result matches the function's intended calculation: the string produces NaN, while true is coerced to 1. section 7.2
Listing 7.8 Displaying the variable type in the index.ts file in the src folder
let myVar;
console.log(`${myVar} = ${typeof myVar}`);
myVar = 12;
console.log(`${myVar} = ${typeof myVar}`);
myVar = "Hello";
console.log(`${myVar} = ${typeof myVar}`);
myVar = true;
console.log(`${myVar} = ${typeof myVar}`);
Trace one variable from undefined to number, string, and boolean values, with typeof output after each assignment. section 7.2





