liveBook · Manning

JavaScript

As explained in Essential TypeScript 5, Third Edition

A dynamic, multi-paradigm programming language standardized as ECMAScript and commonly executed in web browsers and server runtimes.

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

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through coercion and dynamic types, then explains compiler targets, module choices, and JavaScript type declarations.
Examples
Uses mixed-type prices, changing `typeof` results, a tax function, and ES5 compiler output.
What it assumes
Assumes JavaScript knowledge for effective TypeScript use while revisiting its type system and introducing objects, collections, and modules.
What you can do after
Operate/configure it through TypeScript compiler targets and module settings, and diagnose unexpected results from JavaScript’s dynamic types.
How it connects
Connects to TypeScript, static types, ECMAScript, JavaScript modules, Node.js, and type declarations.

Also explained in

Compare how 12 books explain JavaScript →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.