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! πŸš€πŸš€πŸššπŸššπŸšš

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

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

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay