Home > Web Front-end > JS Tutorial > How to use typeof in js

How to use typeof in js

下次还敢
Release: 2024-05-07 18:54:15
Original
640 people have browsed it

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

How to use typeof in js

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>
Copy after login

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:

  • "undefined": undefined variable
  • "boolean": Boolean value
  • "number": number
  • "string": string
  • "object": object (including array)
  • "function": function
  • "symbol": Symbol value

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template