Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 5.56

bookNumber Type

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1
some-alt