JavaScript prerequisites
Concept and syntax
TypeScript builds on JavaScript. Review these building blocks before the next chapter: const binds a name without reassignment; let allows reassignment; an array holds ordered values; an object holds named properties. Use if to choose a branch, functions to reuse work, and === to compare without coercion. Indexing starts at zero. The example uses only JavaScript syntax, which can also appear in a .ts file. If these constructs are unfamiliar, practice them in a JavaScript tutorial before progressing.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
const prices = [4, 8];const item = { name: "tea", price: prices[0] };let total = item.price;if (item.name === "tea") total += 2;console.log(total);Expected output (one line per log):
6Common mistake
Intentionally invalid; check this snippet separately. Do not run it.
Expected compiler diagnostic: TS2588. const prevents rebinding, not all mutation of objects or arrays. Use let when the binding must change.
Practice and recap
Add a third price, then choose its index and change the condition to check another item name. Predict the result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript prerequisites — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
Which index selects the first array element?
What does const prevent?
Which change permits reassignment of total?
const total = 4;total = 6;