Syntax

A programming language's syntax is similar to the grammatical structure of a spoken language. In English, words are expected to be in a certain order ("I like apples", not "Apples I like"). Certain words like "I" are expected to be capitalized a certain way, and sentences are expected to end with a period (.).

In TypeScript, the semicolon (;) is an explicit indicator for the end of an instruction.

Loading TypeScript...

Notice the third line in the program above doesn't have a semicolon. In most situations, a line break will be interpreted as the end of an instruction - this is called Automatic Semicolon Insertion (ASI).

The examples on this site tend to not use semicolons for the sake of clarity and readability. However, there are several situations where a semicolon is required for correctness, such as the program below.

Loading TypeScript...

The expression 42(1 + 1)... is being interpreted as a function call of a nonexistent function named 42. This is because it is perfectly valid to write a console.log instruction like this:

Loading TypeScript...

In the vast majority of TypeScript programs, you will find semicolons at the end of every line. Most professional codebases and big companies require them. At the end of the day, semicolon usage is a matter of stylistic preference - what is most important to understand is that there are situations where a semicolon is absolutely necessary.

Was this page helpful?