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

What is the method to determine data type in js

下次还敢
Release: 2024-05-08 23:21:24
Original
922 people have browsed it

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

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

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

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

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

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

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!

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