Home  >  Article  >  Web Front-end  >  What is the method to determine data type in js

What is the method to determine data type in js

下次还敢
下次还敢Original
2024-05-08 23:21:24851browse

Four ways to determine data type in JavaScript: typeof operator returns value type string instanceof operator checks whether it is an instance of the specified type Object.prototype.toString() method returns value type internal representation string Array. The isArray() method checks whether it is an array

What is the method to determine data type in js

Method to determine data type in JavaScript

In JavaScript, determine The data type has the following methods:

1. typeof operator

typeof operator returns a string representing the given value type. Possible return values ​​include:

  • "undefined": Undefined value.
  • "null": Null value.
  • "number": Number.
  • "bigint": Big integer.
  • "string":String.
  • "boolean": Boolean value.
  • "symbol": Symbol.
  • "object": All other values ​​such as objects, arrays, functions, etc.

Example:

<code class="javascript">console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object"
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof [1, 2, 3]); // "object"</code>

2. instanceof operator

instanceof operator Checks whether the given value is an instance of the specified type. It returns a Boolean value: true for yes, false for no.

Syntax:

<code class="javascript">object instanceof Constructor</code>

Where:

  • object is the value to be checked.
  • Constructor is the constructor or built-in type of the class to be checked.

Example:

<code class="javascript">console.log([] instanceof Array); // true
console.log({} instanceof Object); // true
console.log("hello" instanceof String); // false</code>

3. Object.prototype.toString() method

Object The .prototype.toString() method returns a string representing the internal representation of the given value type. It is usually represented in the following format:

<code>"[object Type]"</code>

where Type is the type of the value.

Example:

<code class="javascript">console.log(Object.prototype.toString.call(undefined)); // "[object Undefined]"
console.log(Object.prototype.toString.call(null)); // "[object Null]"
console.log(Object.prototype.toString.call(42)); // "[object Number]"
console.log(Object.prototype.toString.call("hello")); // "[object String]"
console.log(Object.prototype.toString.call(true)); // "[object Boolean]"
console.log(Object.prototype.toString.call([1, 2, 3])); // "[object Array]"</code>

4. Array.isArray() method

Array.isArray() Method specifically checks whether the given value is an array. It returns a Boolean value: true for yes, false for no.

Example:

<code class="javascript">console.log(Array.isArray([])); // true
console.log(Array.isArray({})); // false</code>

The above is the detailed content of What is the method to determine data type 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