DEV Community

Birusha Ndegeya
Birusha Ndegeya

Posted on

Learn Primitives Types in TypeScript

Learn Primitives Types in TypeScript

Hello there ๐Ÿ‘‹! I am Birusha Ndegeya.

Learning TypeScript is crucial to understanding programming languages. TypeScript, being a superset of JavaScript, can be easily learned if you're familiar with JavaScript fundamentals. You can even choose TypeScript as your first programming language.

In this tutorial, we'll cover the basics of TypeScript, starting with data types. For those new to programming, knowing how to print something on a computer using TypeScript is essential. Let's start with a basic command:


console.log("Hello World"); // Output: Hello World

Enter fullscreen mode Exit fullscreen mode

Let's dive deeper into understanding TypeScript primitive types.

To create a variable, we use the keywords let or const.

let name: string = "Jill";
const ID: number = 1;
Enter fullscreen mode Exit fullscreen mode

To display these variables, we use the function:


console.log(name); // Output: Jill
console.log(ID); // Output: 1

Enter fullscreen mode Exit fullscreen mode

We use const for values that never change, and by convention, we write them in uppercase. let is used when the value can change.

Now, let's explore the basic data types widely used in TypeScript:

  • string : Represents a chain of characters enclosed in double or single quotes.

let firstName: string = "Jimmy";
let lastName: string = 'Elijah';
// We can print them to the screen:
console.log(firstName); // Output: Jimmy
console.log(lastName); // Output: Elijah

// Checking the type:
console.log(typeof firstName); // Output: string
console.log(typeof lastName); // Output: string

Enter fullscreen mode Exit fullscreen mode
  • boolean : Represents true or false values.

const isMale: boolean = true;
const isFemale: boolean = false;

// Printing them to the screen:
console.log(isMale); // Output: true
console.log(isFemale); // Output: false

// Checking the type:
console.log(typeof isMale); // Output: boolean
console.log(typeof isFemale); // Output: boolean

Enter fullscreen mode Exit fullscreen mode
  • number Represents any numeric value, including whole numbers or decimal numbers.

let age: number = 30;
const GPA: number = 3.1;

// Printing them to the screen:
console.log(age); // Output: 30
console.log(GPA); // Output: 3.1

// Checking the type:
console.log(typeof age); // Output: number
console.log(typeof GPA); // Output: number

Enter fullscreen mode Exit fullscreen mode

Additionally, a variable can hold multiple types:


let ID: number | string;
// Assigning it a number value:
ID = 1;
console.log(ID); // Output: 1
// Checking the type:
console.log(typeof ID); // Output: number

// Assigning it a string value:
ID = '01';
console.log(ID); // Output: 01

// Checking the type:
console.log(typeof ID); // Output: string

Enter fullscreen mode Exit fullscreen mode

Now, if you need a starter code, you can visit my GitHub profile.

Thanks for keeping up with the reading.

Happy coding! ๐Ÿš€๐Ÿš€๐Ÿšš๐Ÿšš๐Ÿšš

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

๐Ÿ‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Communityโ€”every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple โ€œthank youโ€ goes a long wayโ€”express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay