Menu
Coddy logo textTech

Your First TypeScript Code

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 3 of 73.

Now it's time to write your first TypeScript code! The key difference between TypeScript and JavaScript is the ability to add type annotations to your variables.

A type annotation tells TypeScript exactly what kind of data a variable should hold. The syntax is simple: you add a colon followed by the type name after the variable name.

let variableName: type = value;

For example, to create a variable that can only hold text (strings), you would write:

let message: string = "Hello, World!";

The : string part is the type annotation. It explicitly tells TypeScript that message should only contain string values.

If you try to assign a number or any other type to this variable later, TypeScript will catch the error before your code runs.

This explicit typing is what makes TypeScript so powerful for catching bugs early and making your code more predictable.

challenge icon

Challenge

Easy

Create a variable named greeting with an explicit type annotation of string and assign it the value "Hello, TypeScript!".

Then, print the variable to the console using console.log().

Try it yourself

// TODO: Write your code here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript