JavaScript is the code a browser or Node.js runtime executes after a TypeScript project is compiled. TypeScript adds static types to JavaScript, but the compiler emits pure JavaScript; a runtime is still required. This also lets TypeScript projects use JavaScript packages, often with bundled or separate type declarations. section 2.6 section 15.5
JavaScript statements execute in order. Use `let` for a variable, `const` for a constant, and `console.log` to print a value. Template strings use backticks and insert expressions with `${...}`. These familiar statements can produce surprising results when values have different types. section 3.2
Listing 3.6 Adding statements in the index.ts 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 = hatPrice + bootsPrice;
console.log(`Total Price: ${totalPrice}`);
Compare the numeric `hatPrice` with the string `bootsPrice`: the comparison succeeds, but adding them produces the concatenated string `100100`, not a numeric total. section 3.2
JavaScript is dynamically typed: values have types, while a variable can hold different types as assignments change. `typeof` reports an unassigned variable as `undefined`, then reports `number`, `string`, or `boolean` after corresponding assignments. Function parameters can likewise receive any of these values. A tax calculation written for numbers produces `NaN` for `"Hello"` and uses `true` as 1; static types help catch such mismatches before execution. section 7.2
Two practical choices shape the JavaScript that a TypeScript project runs:
- Set `target` in `tsconfig.json` to select the output language version. An ES5 target changes modern syntax such as `let` and arrow functions into older forms for older runtimes. section 5.6
- Choose a module format so code can be split across files. ECMAScript modules are the recommended choice when the runtime supports them; CommonJS is an older format associated with Node.js. section 4.5
Pure JavaScript files can remain in a TypeScript project. The compiler can check them, but its type inference is limited without declarations; declarations can describe local code or third-party packages. Compiler settings keep source files separate from generated output and can select a module format using `package.json`. section 15.5 section 15.1











