Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the JavaScript typeof operator

Shulou Source: shulou.com Published: 2022-06-01 05:22:26 10月06日 Update

This article mainly explains "how to use the JavaScript typeof operator". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the JavaScript typeof operator.

Typeof operator

You can use the typeof operator to determine the data type of the JavaScript variable.

Example

Typeof "Bill" / / returns "string"

Typeof 3.14 / / returns "number"

Typeof NaN / / returns "number"

Typeof false / / returns "boolean"

Typeof [1, 2, 3, 4] / / return "object"

Typeof {name:'Bill', age:62} / / returns "object"

Typeof new Date () / / returns "object"

Typeof function () {} / / returns "function"

Typeof myCar / / returns "undefined" *

Typeof null / / returns "object"

Please note:

The data type of NaN is numeric

The data type of the array is an object

The data type of the date is the object

The data type of null is an object

The data type of an undefined variable is undefined

Variables that have not yet been assigned also have the data type undefined

You cannot use typeof to determine whether the JavaScript object is an array (or date).

Data type of typeof

The typeof operator is not a variable. It belongs to the operator. Operators, such as +-* /, have no data type.

However, typeof always returns a string (the type that contains operands).

Constructor attribute

The constructor property returns the constructor functions for all JavaScript variables.

Example

"Bill" .constructor / / returns "function String () {[native code]}"

Constructor / / returns "function Number () {[native code]}"

False.constructor / / returns "function Boolean () {[native code]}"

] .constructor / / returns "function Array () {[native code]}"

{name:'Bill', age:62} .constructor / / returns "function Object () {[native code]}"

New Date () .constructor / / returns "function Date () {[native code]}"

Function () {} .constructor / / returns "function Function () {[native code]}"

You can determine whether an object is an array (including the word "Array") by checking the constructor property:

Example

Function isArray (myArray) {

Return myArray.constructor.toString () .indexOf ("Array") >-1

}

Or more simply, you can check whether the object is an array function:

Example

Function isArray (myArray) {

Return myArray.constructor = Array

}

You can determine whether an object is a date (including the word "Date") by checking the constructor property:

Example

Function isDate (myDate) {

Return myDate.constructor.toString () .indexOf ("Date") >-1

}

Or more simply, you can check whether the object is a date function:

Example

Function isDate (myDate) {

Return myDate.constructor = Date

}

Thank you for your reading, the above is the content of "how to use the JavaScript typeof operator". After the study of this article, I believe you have a deeper understanding of how to use the JavaScript typeof operator, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Operations types operators data objects instances variables attributes arrays dates checks functions learning content words can be passed characters strings not yet just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei OPPO Reno vpn Shulou Tech Info