Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Primitives in TypeScript?

In this blog, we will be discussing a fundamental concept in TypeScript known as "primitives." If you're new to programming or just beginning to learn TypeScript, this article is for you! We will go over what primitives are, why they are essential in programming, and how to use them in TypeScript, along with several examples. By the end of this article, you'll have a better understanding of primitive types and how to work with them in your TypeScript projects.

What are Primitives?

In programming, a "primitive" is a basic data type that is used to represent simple data or values. Think of them as the building blocks of your code. These basic data types are the simplest form of data that a programming language can understand and manipulate. They are also the foundation upon which more complex data structures are built.

In TypeScript, there are six main primitive types:

  1. Boolean: Represents true or false values.
  2. Number: Represents numeric values, including integers and floating-point numbers.
  3. String: Represents a sequence of characters.
  4. Symbol: Represents a unique, non-numeric value. (Introduced in ECMAScript 2015)
  5. Null: Represents the absence of any value.
  6. Undefined: Represents a variable that has not been assigned a value.

Let's go through each of these primitive types one by one to better understand them and see how they can be used in TypeScript.

1. Boolean

In TypeScript, the boolean primitive is used to represent true or false values. Booleans are typically used in conditional statements and logic operations.

Here's a simple example of how to declare a boolean variable in TypeScript:

let isHappy: boolean = true;

In this example, we declare a variable named isHappy with the type boolean and assign it the value true.

We can also use booleans in conditional statements, like this:

if (isHappy) {
    console.log("I am happy!");
} else {
    console.log("I am not happy!");
}

In this case, the output will be "I am happy!" because the value of isHappy is true.

2. Number

In TypeScript, the number primitive is used to represent numeric values. This includes both integers (whole numbers) and floating-point numbers (numbers with a decimal point). TypeScript uses the IEEE 754 standard for representing numbers, which is the same standard used by JavaScript.

Here's an example of how to declare a number variable in TypeScript:

let age: number = 26;

In this example, we declare a variable named age with the type number and assign it the value 26.

We can also perform arithmetic operations with number variables, like this:

let doubleAge: number = age * 2;
console.log(doubleAge); // Output: 52

In this case, we multiply the value of age (which is 26) by 2, and the result is 52.

3. String

In TypeScript, the string primitive is used to represent sequences of characters. Strings are typically used to store and manipulate text data.

Here's an example of how to declare a string variable in TypeScript:

let name: string = "Alice";

In this example, we declare a variable named name with the type string and assign it the value "Alice".

We can also concatenate (join) strings using the + operator, like this:

let greeting: string = "Hello, " + name + "!";
console.log(greeting); // Output: Hello, Alice!

In this case, we join the string "Hello, " with the value of name (which is "Alice") and the string "!". The result is the string "Hello, Alice!".

4. Symbol

In TypeScript, the symbol primitive is used to represent unique, non-numeric values. Symbols are created by calling the Symbol() function and can be used as unique property keys in objects.

Here's an example of how to declare a symbol variable in TypeScript:

let uniqueKey: symbol = Symbol("key");

In this example, we declare a variable named uniqueKey with the type symbol and assign it a new symbol with the description "key".

We can use symbols as keys in objects, like this:

let obj: { [key: symbol]: string } = {};
obj[uniqueKey] = "Hello, World!";
console.log(obj[uniqueKey]); // Output: Hello, World!

In this case, we create an object named obj with a symbol-keyed property, and we assign it the value "Hello, World!". When we access the property using the symbol uniqueKey, the output is "Hello, World!".

5. Null

In TypeScript, the null primitive represents the absence of any value. It is often used to indicate that a variable should have no value or that an object property does not exist.

Here's an example of how to declare a variable with the null value in TypeScript:

let empty: null = null;

In this example, we declare a variable named empty with the type null and assign it the value null.

We can also use null to represent the absence of a value in more complex types, like this:

let person: { name: string; age: number | null } = {
  name: "Bob",
  age: null,
};

In this case, we create an object named person with a name property of type string and an age property of type number or null. We assign the age property the value null, indicating that the age is not specified or not applicable.

6. Undefined

In TypeScript, the undefined primitive represents a variable that has not been assigned a value. When you declare a variable without assigning a value, its default value is undefined.

Here's an example of how to declare a variable with an undefined value in TypeScript:

let notAssigned: undefined = undefined;

In this example, we declare a variable named notAssigned with the type undefined and assign it the value undefined.

We can also use undefined to represent the absence of a value in more complex types, like this:

let optional: { name: string; age?: number } = {
  name: "Carol",
};

In this case, we create an object named optional with a name property of type string and an optional age property of type number. We do not assign a value to the age property, so its value is undefined.

Conclusion

In this article, we discussed the concept of primitives in TypeScript and went through each of the six main primitive types: boolean, number, string, symbol, null, and undefined. By understanding these fundamental data types, you can build more complex data structures and write more efficient and reliable TypeScript code.

Now that you have a good grasp of these basic building blocks, you can continue exploring TypeScript and other programming concepts to become a more proficient programmer. Happy coding!