The typeof operator in JavaScript is used to determine the type of a value. It returns a string: "undefined": undefined variable "boolean": Boolean value "number": number "string": character String "object": object (including array) "function": function "symbol": Symbol value
typeof operator in JavaScript
The typeof operator is used to determine the data type of a JavaScript value. The syntax is as follows:
<code class="javascript">typeof value;</code>
Among them, value is the variable or expression to determine the data type.
Return value
The typeof operator returns a string indicating the type of the value. Possible return values include:
Example
<code class="javascript">const x = 10; console.log(typeof x); // 输出: "number" const y = "hello"; console.log(typeof y); // 输出: "string" const z = [1, 2, 3]; console.log(typeof z); // 输出: "object" const f = function() {}; console.log(typeof f); // 输出: "function" const s = Symbol("foo"); console.log(typeof s); // 输出: "symbol"</code>
It is worth noting that the return value of typeof operator for null is "object". This is because in JavaScript, null is treated as an object, not a primitive type.
The above is the detailed content of How to use typeof in js. For more information, please follow other related articles on the PHP Chinese website!