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

How to use typeof in js

下次还敢
下次还敢Original
2024-05-01 08:54:171035browse

typeof operator returns the type of operand, its syntax is: typeof operand. It returns one of the following string types: "undefined", "null", "boolean", "number", "bigint", "string", "symbol", or "object". Returning null "object" is a historical issue. Composite data structure

How to use typeof in js

Usage of typeof operator in JavaScript

# The ##typeof operator is a unary operator that returns a string indicating the type of the operand.

Syntax:

<code>typeof operand</code>

Parameters:

  • operand: To determine its type expression or variable.

Return value:

A string representing the type of

operand:

  • "undefined": The value is undefined.
  • "null": The value is null.
  • "boolean": The value is a Boolean value.
  • "number": The value is a number.
  • "bigint": The value is a big integer.
  • "string": The value is a string.
  • "symbol": The value is a Symbol value.
  • "object": The value is an object, including functions, arrays and regular expressions.

Example:

<code class="js">console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object"
console.log(typeof true); // "boolean"
console.log(typeof 123); // "number"
console.log(typeof "Hello World"); // "string"
console.log(typeof Symbol("Symbol")); // "symbol"
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof function() {}); // "function"</code>

Note: The

    typeof operator only returns JavaScript primitive types The name, not the type of the custom object.
  • null Returning "object" is a historical issue. It should return "null", but this cannot be changed due to backward compatibility.
  • If
  • operand is a composite data structure, such as an array or object, the typeof operator will return "object".
  • The typeof operator can be used to determine whether a variable has been declared, even if it has not been assigned a value.

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!

Statement:
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