Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Number Type | Core TypeScript Types
TypeScript Foundations

bookNumber Type

TypeScript uses the number type to represent both integer and floating-point values. This means you do not need to distinguish between whole numbers and decimals—TypeScript treats them all as number. You can use the number type for quantities like age, price, temperature, or any other numeric value.

123456789101112131415
// Declaring number variables let age: number = 30; // integer value let price: number = 19.99; // floating-point value // Arithmetic operations let total: number = age + price; let difference: number = age - price; let product: number = age * 2; let quotient: number = age / 3; // Output values console.log('Total:', total); // Total: 49.99 console.log('Difference:', difference); // Difference: 10.01 console.log('Product:', product); // Product: 60 console.log('Quotient:', quotient); // Quotient: 10
copy

TypeScript can often figure out the type of a variable automatically. This is called type inference. For example, if you write let temperature = 72;, TypeScript knows that temperature is a number because you assigned a numeric value. However, you can also use an explicit type annotation, such as let temperature: number = 72;, to make your intent clear. Both approaches are valid, but explicit annotations can help make your code easier to read and understand, especially in larger projects.

question mark

Which of the following is the correct way to declare a variable with the number type in TypeScript?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1

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 5.56

bookNumber Type

Sveip for å vise menyen

TypeScript uses the number type to represent both integer and floating-point values. This means you do not need to distinguish between whole numbers and decimals—TypeScript treats them all as number. You can use the number type for quantities like age, price, temperature, or any other numeric value.

123456789101112131415
// Declaring number variables let age: number = 30; // integer value let price: number = 19.99; // floating-point value // Arithmetic operations let total: number = age + price; let difference: number = age - price; let product: number = age * 2; let quotient: number = age / 3; // Output values console.log('Total:', total); // Total: 49.99 console.log('Difference:', difference); // Difference: 10.01 console.log('Product:', product); // Product: 60 console.log('Quotient:', quotient); // Quotient: 10
copy

TypeScript can often figure out the type of a variable automatically. This is called type inference. For example, if you write let temperature = 72;, TypeScript knows that temperature is a number because you assigned a numeric value. However, you can also use an explicit type annotation, such as let temperature: number = 72;, to make your intent clear. Both approaches are valid, but explicit annotations can help make your code easier to read and understand, especially in larger projects.

question mark

Which of the following is the correct way to declare a variable with the number type in TypeScript?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt