Skip to content
BENIBIX Learn

JavaScript and TypeScript

Level 2 · Languages of the web

The one language every web browser runs, and TypeScript, which adds types to it. What it checks, and why it turns back into JavaScript.

Course contentsLesson 5 of 12

Level 3 · General-purpose and systems languages

  1. Python, Java and C#
  2. C, C++, Rust and Go

Level 4 · Languages for phones

  1. Swift, Kotlin and Dart

Level 5 · Data and functional languages

  1. R, Julia and MATLAB
  2. Haskell, Scala and Elixir

Level 6 · Scripting languages

  1. Shell Scripts, Perl and Lua

After this lesson you can

  • Say why every web page uses JavaScript
  • Explain what TypeScript adds
  • Say what happens to TypeScript before it runs

12 mincovers 2 glossary entries

Worth reading first: Static and Dynamic Typing

Why the web runs on JavaScript, and what TypeScript adds

JavaScript is the only language that every web browser runs on its own. It was made in 1995, for one of the first web browsers, to make pages react to clicks and typing. Today it does far more. Menus that open, forms that check what you typed, and maps you can drag are all JavaScript running in your browser.

JavaScript is not just for browsers any more. Tools like Node.js run it on servers too, so one language can build both halves of a web app. Its name is a leftover from 1990s marketing. It has almost nothing to do with Java.

JavaScript is dynamically and weakly typed. That makes small scripts quick to write. In a big app it lets mistakes slip through, such as passing text where a number was expected. The page still loads, and the bug shows up later, in a user's browser.

TypeScript is JavaScript with types added. Microsoft made it public in 2012. You write let count: number = 0, and the TypeScript tools check every use of count before the code runs. Pass it text and you see the error in your editor, not in a user's browser.

Browsers do not run TypeScript. So a build step strips the types out and leaves plain JavaScript. The types are only there to catch mistakes while you write. Every JavaScript file is already valid TypeScript. So a team can switch over one file at a time.

Which to learn? Start with JavaScript, because TypeScript is JavaScript plus types and you need the base first. Most large web teams now write TypeScript. A small script or a quick page is often fine in plain JavaScript.

The common mistake is thinking TypeScript makes the code run faster or safer for users. It changes nothing at run time, because the types are gone by then. It makes the code safer to write and to change.

typescript
function total(price: number, qty: number): number {
  return price * qty;
}

total(2.5, 4);      // 10
total("2.5", 4);    // error before it runs:
// Argument of type 'string' is not assignable
// to parameter of type 'number'.
TypeScript spots the text in the second call while you type. Plain JavaScript would run it and give a wrong answer.
Types catch the mistake while you write. Then they are stripped, and the browser runs plain JavaScript.

Worked example

Lucy's team moves to TypeScript

Lucy's team has 300 JavaScript files. They rename 20 of the busiest to TypeScript first, and the type check finds 14 hidden bugs in a day.

Over 3 months they move the rest, one file at a time. The app keeps working the whole time, since old files are fine as they are.

The words for this lesson2

Check yourself

What is done to TypeScript before a browser can run it?

Finished this lesson?

Marking it complete also marks its 2 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.