Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the JavaScript typeof operator

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report