Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Primitive Types | Understanding TypeScript Types
TypeScript Types Fundamentals

bookPrimitive Types

TypeScript provides several primitive types that represent the most basic forms of data you can work with. These include string, number, boolean, bigint, symbol, and undefined. You use these types to describe simple values and to help TypeScript catch mistakes in your code.

1234567891011121314151617
// String type let firstName: string = "Alice"; // Number type let age: number = 30; // Boolean type let isActive: boolean = true; // BigInt type let bigNumber: bigint = 9007199254740991n; // Symbol type let uniqueId: symbol = Symbol("id"); // Undefined type let notAssigned: undefined = undefined;
copy

The string type is used for text data. In the example above, firstName is declared as a string and assigned the value "Alice". This means firstName can only hold text values.

The number type covers all numeric values, including integers and floating-point numbers. The variable age is a number and is set to 30. You can use number for calculations, measurements, and any other numeric data.

For logical values, TypeScript uses the boolean type. The variable isActive is a boolean and is assigned true. You use boolean to represent true/false or yes/no conditions in your code.

The bigint type is for very large integers that cannot be represented by the regular number type. The variable bigNumber is a bigint, which you create by adding an n to the end of the number. Use bigint when you need to work with extremely large whole numbers.

Symbols are unique and immutable values, and are represented by the symbol type. The variable uniqueId is a symbol, created using the Symbol() function. Symbols are often used as unique keys for object properties.

Finally, undefined is a type that represents a variable that has not been assigned a value. The variable notAssigned is explicitly set to undefined. In TypeScript, a variable that is declared but not initialized will also have the type undefined by default.

By using these primitive types, you can write safer TypeScript code and take advantage of strong type checking to prevent common errors.

question mark

Which of the following statements about TypeScript's primitive types is true?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 8.33

bookPrimitive Types

Sveip for å vise menyen

TypeScript provides several primitive types that represent the most basic forms of data you can work with. These include string, number, boolean, bigint, symbol, and undefined. You use these types to describe simple values and to help TypeScript catch mistakes in your code.

1234567891011121314151617
// String type let firstName: string = "Alice"; // Number type let age: number = 30; // Boolean type let isActive: boolean = true; // BigInt type let bigNumber: bigint = 9007199254740991n; // Symbol type let uniqueId: symbol = Symbol("id"); // Undefined type let notAssigned: undefined = undefined;
copy

The string type is used for text data. In the example above, firstName is declared as a string and assigned the value "Alice". This means firstName can only hold text values.

The number type covers all numeric values, including integers and floating-point numbers. The variable age is a number and is set to 30. You can use number for calculations, measurements, and any other numeric data.

For logical values, TypeScript uses the boolean type. The variable isActive is a boolean and is assigned true. You use boolean to represent true/false or yes/no conditions in your code.

The bigint type is for very large integers that cannot be represented by the regular number type. The variable bigNumber is a bigint, which you create by adding an n to the end of the number. Use bigint when you need to work with extremely large whole numbers.

Symbols are unique and immutable values, and are represented by the symbol type. The variable uniqueId is a symbol, created using the Symbol() function. Symbols are often used as unique keys for object properties.

Finally, undefined is a type that represents a variable that has not been assigned a value. The variable notAssigned is explicitly set to undefined. In TypeScript, a variable that is declared but not initialized will also have the type undefined by default.

By using these primitive types, you can write safer TypeScript code and take advantage of strong type checking to prevent common errors.

question mark

Which of the following statements about TypeScript's primitive types is true?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
some-alt