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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain more about type inference in TypeScript?

When should I use explicit type annotations instead of relying on type inference?

Are there any other numeric types in TypeScript besides `number`?

Awesome!

Completion rate improved to 5.56

bookNumber Type

Glissez pour afficher le menu

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1
some-alt