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 determine the type of JavaScript variable

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to determine the type of JavaScript variable related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will get something after reading this article on how to determine the type of JavaScript variable, let's take a look at it.

You can use JavaScript's typeof to determine the type of JavaScript variable:

The typeof operator returns the type of variable or expression:

Example

Typeof "" / / returns "string"

Typeof "Bill" / / returns "string"

Typeof "Bill Gates" / / returns "string"

Example

Typeof 0 / / returns "number"

Typeof 314 / / returns "number"

Typeof 3.14 / / returns "number"

Typeof (7) / / returns "number"

Typeof (7 + 8) / / returns "number"

The typeof operator returns "object" to the array, because the array belongs to the object in JavaScript.

Undefined

In JavaScript, a variable with no value whose value is undefined. Typeof also returns undefined.

Example

Var person; / / value is undefined and type is undefined

Any variable can be cleared by setting the value to undefined. Its type will also be undefined.

Example

Person = undefined; / / the value is undefined and the type is undefined

Null value

Null is not the same thing as undefined.

Empty string variables have both values and types.

Example

Var car = ""; / / the value is "" and the type is "string"

Null

In JavaScript, null is "nothing". It is seen as something that does not exist.

Unfortunately, in JavaScript, the data type of null is an object.

You can think of null as an object in JavaScript as a bug. It was supposed to be null.

You can clear the object by setting the value for null:

Example

Var person = null; / / value is null, but the type is still an object

You can also clear the object for undefined by setting the value:

Example

Var person = undefined; / / the value is undefined and the type is undefined

The difference between Undefined and Null

The values of Undefined and null are equal, but the types are not equal:

Typeof undefined / / undefined

Typeof null / / object

Null = undefined / / false

Null = = undefined / / true

This is the end of the article on "how to determine the type of JavaScript variable". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to determine the type of JavaScript variable". If you want to learn more, you are welcome to follow the industry information channel.

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