What is the basic type used for numbers in TypeScript?
TypeScript uses the number type to represent all numeric values, including integers and floating-point numbers.
Click to reveal answer
beginner
What happens when you perform arithmetic operations with number types in TypeScript?
Arithmetic operations like addition, subtraction, multiplication, and division work as expected on number types, following JavaScript's number rules.
Click to reveal answer
intermediate
How does TypeScript handle special numeric values like NaN and Infinity?
NaN (Not a Number) and Infinity are special number values in TypeScript, inherited from JavaScript, used to represent invalid or unbounded results.
Click to reveal answer
intermediate
Can TypeScript distinguish between integers and floating-point numbers with the number type?
No, TypeScript's number type does not differentiate between integers and floating-point numbers; both are treated as the same type.
Click to reveal answer
advanced
What is the range of safe integers in TypeScript's number type?
The safe integer range is from Number.MIN_SAFE_INTEGER (−(253 − 1)) to Number.MAX_SAFE_INTEGER (253 − 1). Outside this range, precision may be lost.
Click to reveal answer
Which type does TypeScript use for all numeric values?
Aint
Bnumber
Cfloat
Ddecimal
✗ Incorrect
TypeScript uses the 'number' type for all numeric values, including integers and floats.
What is the result of dividing a positive number by zero in TypeScript?
ANaN
BError
C0
DInfinity
✗ Incorrect
Dividing a positive number by zero results in 'Infinity' in TypeScript.
Does TypeScript's number type distinguish between integers and floating-point numbers?
ANo, both are the same type
BOnly for integers
CYes, it has separate types
DOnly for floating-point numbers
✗ Incorrect
TypeScript treats both integers and floating-point numbers as the same 'number' type.
What does NaN represent in TypeScript?
AZero
BA valid number
CNot a Number (invalid numeric result)
DInfinity
✗ Incorrect
NaN means 'Not a Number', representing an invalid numeric result.
What is the significance of Number.MAX_SAFE_INTEGER?
AMaximum integer value that can be safely represented without precision loss
BMaximum number allowed in TypeScript
CMaximum floating-point value
DMaximum array length
✗ Incorrect
Number.MAX_SAFE_INTEGER is the largest integer that can be safely represented without losing precision.
Explain how TypeScript handles the number type and its special values like NaN and Infinity.
Think about how JavaScript treats numbers and special cases.
You got /4 concepts.
Describe the concept of safe integers in TypeScript and why it matters.
Consider what happens when numbers get very big.
You got /3 concepts.
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.