Ad space (banner)
Lesson 2 / 20

Your First Output (console.log)

This lesson covers the basics of output in TypeScript, plus your first taste of type annotations. It's for anyone searching "TypeScript console.log tutorial."

console.log() prints text to the screen (the console) โ€” written exactly the same way as in JavaScript. It helps to think of TypeScript as "JavaScript with a type-checking feature added on top."

The example writes let message: string = "Hello, TypeScript!";, adding a type annotation, : string, right after the variable name. This is how you declare to TypeScript: "only strings go into this variable."

A common early mistake is assuming you have to write a type every single time โ€” you don't. TypeScript actually infers the type automatically from the assigned value, so : string can be omitted and the code still works. It's still worth practicing writing types explicitly at first, until it feels natural.

In real projects, having types means your editor can offer much stronger autocomplete and catch typos as you type, which is why so many large JavaScript projects have migrated to TypeScript.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)