0
0
Javascriptprogramming~10 mins

Type checking using typeof in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type checking using typeof
Start
Evaluate expression
Apply typeof operator
Return type as string
Use type info (e.g., if, print)
End
The program evaluates a value, applies the typeof operator to get its type as a string, then uses that type information.
Execution Sample
Javascript
let x = 42;
console.log(typeof x);

let y = 'hello';
console.log(typeof y);
This code checks the types of variables x and y and prints them.
Execution Table
StepExpressiontypeof ResultActionOutput
1x = 42numberAssign 42 to x
2typeof xnumberCheck type of xnumber
3console.log(typeof x)numberPrint type of xnumber
4y = 'hello'stringAssign 'hello' to y
5typeof ystringCheck type of ystring
6console.log(typeof y)stringPrint type of ystring
7End of code
💡 All expressions evaluated, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
xundefined424242
yundefinedundefined'hello''hello'
Key Moments - 2 Insights
Why does typeof return a string like "number" instead of the value itself?
Because typeof always returns the type of the value as a string, not the value. See execution_table step 2 and 5 where typeof x and typeof y return "number" and "string".
What happens if we use typeof on a variable that is not defined?
Using typeof on an undeclared variable returns "undefined" without error. This is safe unlike directly accessing the variable. This is not shown here but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of typeof x at step 2?
A"string"
B"number"
C42
Dundefined
💡 Hint
Check the 'typeof Result' column at step 2 in the execution_table.
At which step is the variable y assigned a value?
AStep 4
BStep 5
CStep 1
DStep 6
💡 Hint
Look at the 'Action' column for assignment actions in the execution_table.
If we changed x to true, what would typeof x return?
A"string"
B"number"
C"boolean"
D"object"
💡 Hint
Recall typeof returns the type as a string; true is a boolean type.
Concept Snapshot
typeof operator returns the type of a value as a string.
Syntax: typeof value
Common results: "number", "string", "boolean", "undefined", "object", "function".
Useful for checking variable types before using them.
Safe to use on undeclared variables (returns "undefined").
Full Transcript
This visual execution shows how the typeof operator works in JavaScript. First, variables x and y are assigned values 42 and 'hello'. Then, typeof is applied to each variable, returning their types as strings: "number" for x and "string" for y. These results are printed to the console. The variable tracker shows how x and y change from undefined to their assigned values. Key moments clarify that typeof returns a string describing the type, not the value itself, and that using typeof on undeclared variables is safe. The quiz tests understanding of the typeof results and variable assignments.