📖 Scenario: Imagine you are working with numbers in a simple calculator app. You want to understand how numbers behave in TypeScript, including integers, decimals, and special values like Infinity and NaN.
🎯 Goal: You will create variables with different number types, use a helper variable, perform calculations, and finally print the results to see how TypeScript handles these numbers.
📋 What You'll Learn
Create variables with integer, decimal, Infinity, and NaN values
Add a helper variable to hold a multiplier
Use arithmetic operations to calculate new values
Print the final results to the console
💡 Why This Matters
🌍 Real World
Understanding number behavior is important when building calculators, games, or any app that does math calculations.
💼 Career
Many programming jobs require handling numbers correctly, especially when working with financial data, scientific calculations, or user input validation.
Progress0 / 4 steps
1
Create number variables
Create four variables called integerNum, decimalNum, infinityNum, and nanNum with these exact values: 42, 3.14, Infinity, and NaN respectively.
Typescript
Hint
Use let to declare each variable and assign the exact number values given.
2
Add a multiplier variable
Create a variable called multiplier and set it to 2.
Typescript
Hint
Use let multiplier = 2; to create the helper variable.
3
Calculate new values
Create four new variables called intResult, decResult, infResult, and nanResult. Multiply integerNum, decimalNum, infinityNum, and nanNum by multiplier respectively and assign the results to these new variables.
Typescript
Hint
Use the * operator to multiply each original number by multiplier.
4
Print the results
Use four console.log statements to print intResult, decResult, infResult, and nanResult in this exact order.
Typescript
Hint
Use console.log(variableName); to print each result on its own line.
Practice
(1/5)
1.
In TypeScript, which of the following is true about the number type?
easy
A. It represents both whole numbers and decimals using the same type.
B. It only represents whole numbers (integers).
C. It only represents decimal numbers (floats).
D. You must declare separate types for integers and decimals.
Solution
Step 1: Understand TypeScript number type
TypeScript uses a single number type for all numeric values, whether whole or decimal.
Step 2: Compare options with this fact
Only "It represents both whole numbers and decimals using the same type." correctly states that both whole and decimal numbers share the same number type.
Final Answer:
It represents both whole numbers and decimals using the same type. -> Option A
Quick Check:
TypeScript number = whole + decimal [OK]
Hint: Remember: one number type covers all numeric values [OK]
Common Mistakes:
Thinking integers and decimals have separate types
Assuming number type is only for floats
Believing you must declare int or float explicitly
2.
Which of the following is the correct way to declare a variable x as a number with value 42 in TypeScript?
?
easy
A. let x: number = "42";
B. let x = "42";
C. let x: number = 42;
D. let x: string = 42;
Solution
Step 1: Check variable declaration syntax
In TypeScript, to declare a number variable, use let x: number = value;.
Step 2: Validate each option
let x: number = 42; correctly declares x as a number with value 42. Other options use wrong types or values, such as assigning a string to a number type, which is invalid.
Final Answer:
let x: number = 42; -> Option C
Quick Check:
Correct number declaration = let x: number = 42; [OK]
Hint: Use let x: number = value; for numbers [OK]
Common Mistakes:
Assigning string values to number variables
Missing the type annotation when needed
Using quotes around numeric values
3.
What is the output of the following TypeScript code?
const a: number = 5;
const b: number = 2.5;
const c = a + b;
console.log(c);
medium
A. 7
B. Error
C. 52.5
D. 7.5
Solution
Step 1: Add the two numbers
Variable a is 5 (integer), b is 2.5 (decimal). Adding gives 5 + 2.5 = 7.5.