While writing code, it can be useful to keep track of the data types of the variables in your program. If you need to check the data type of a variable’s value, you can use the
typeof operator checks the value to its right and returns, or passes back, a string of the data type.
const unknown1 = 'foo'; console.log(typeof unknown1); // Output: string const unknown2 = 10; console.log(typeof unknown2); // Output: number const unknown3 = true; console.log(typeof unknown3); // Output: boolean
Let’s break down the first example. Since the value
'foo', a string,
typeof unknown1 will return
console.log() to print the
Great, now let’s check what happens if we reassign the variable.
console.log() statement, reassign
Since you assigned this new value to
newVariable, it has a new type! On the line below your reassignment, use
console.log() to print
typeof newVariable again.