0
0
Typescriptprogramming~10 mins

Number type behavior in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Number type behavior
Declare number variable
Assign numeric value
Perform arithmetic or comparison
Check for special values: NaN, Infinity
Use value in expressions or output
This flow shows how a number variable is declared, assigned, used in calculations, checked for special cases, and then output or used further.
Execution Sample
Typescript
let a: number = 5;
let b: number = 0 / 0;
let c: number = 1 / 0;
console.log(a, b, c);
This code declares three numbers: a normal number, NaN from 0/0, and Infinity from 1/0, then prints them.
Execution Table
StepCode LineVariableValue AssignedSpecial CaseOutput
1let a: number = 5;a5No
2let b: number = 0 / 0;bNaNYes (Not a Number)
3let c: number = 1 / 0;cInfinityYes (Infinity)
4console.log(a, b, c);---5 NaN Infinity
💡 All variables assigned; console.log outputs the values including special number cases.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aundefined5555
bundefinedundefinedNaNNaNNaN
cundefinedundefinedundefinedInfinityInfinity
Key Moments - 3 Insights
Why does dividing 0 by 0 result in NaN instead of a number?
Dividing 0 by 0 is undefined mathematically, so JavaScript/TypeScript assigns NaN (Not a Number) as shown in step 2 of the execution_table.
Why is 1 divided by 0 Infinity and not an error?
In JavaScript/TypeScript, dividing a positive number by zero results in Infinity, a special numeric value, as shown in step 3 of the execution_table.
Can NaN be compared like normal numbers?
No, NaN is unique: it is not equal to anything, even itself. This special behavior is important to remember when checking values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of variable b?
A0
BNaN
CInfinity
Dundefined
💡 Hint
Check the 'Value Assigned' column for step 2 in the execution_table.
At which step does variable c get assigned Infinity?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Variable' and 'Value Assigned' columns in the execution_table.
If we changed 'let b = 0 / 0;' to 'let b = 1 / 0;', what would be the value of b after step 2?
ANaN
B0
CInfinity
Dundefined
💡 Hint
Refer to how variable c is assigned Infinity at step 3 in the execution_table.
Concept Snapshot
Number type in TypeScript holds numeric values including special ones.
Assign numbers directly or from expressions.
Special values: NaN (not a number), Infinity (division by zero).
NaN is unique: not equal to anything, even itself.
Use console.log to see number values including special cases.
Full Transcript
This example shows how TypeScript handles number variables. First, a normal number 5 is assigned to variable a. Then, dividing zero by zero results in NaN, assigned to b. Dividing one by zero results in Infinity, assigned to c. Finally, all three values are printed. NaN means 'not a number' and is a special value for undefined math results. Infinity represents a value larger than any number. These special values behave differently from normal numbers, for example, NaN is not equal to itself. Understanding these helps avoid bugs when working with numbers.