When defining Javascript variables we do not need to define type.
To determine the data type of a given variable, use the typeof
operator.
Javascript typeof
tells you the type information for a variable.
Using the typeof operator on a value returns one of the following strings:
Returned String | Meaning |
---|---|
"undefined" | if the value is undefined |
"boolean" | if the value is a Boolean |
"string" | if the value is a string |
"number" | if the value is a number |
"object" | if the value is an object or null |
"function" | if the value is a function |
"symbol" | if the value is a Symbol |
The typeof operator is called like this:
let message = "some string"; console.log(typeof message); // "string" console.log(typeof(message)); // "string" console.log(typeof 95); // "number"
typeof
is an operator and not a function, no parentheses are required.
Calling "typeof null" returns a value of "object", as null is an empty object reference.