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

JavaScript Boolean conversion value, date conversion to number and automatic string conversion method

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

Share

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

This article mainly introduces JavaScript Boolean conversion values, date conversion to numbers and automatic string conversion methods related knowledge, detailed and easy to understand, simple and fast operation, with a certain reference value, I believe everyone read this JavaScript Boolean conversion values, date conversion to numbers and automatic string conversion methods articles will have some gains, let's take a look at it.

JavaScript Boolean to numeric, date to numeric, automatic string conversion

The global method Number() can also convert booleans to numbers.

Number(false) //returns 0

Number(true) //returns 1

Convert dates to numbers

The global method Number() can be used to convert dates to numbers.

d = new Date();

Number(d) //returns 1640921072728

The date method getTime() is the same.

d = new Date();

d.getTime() //returns 1640921072728

automatic type conversion

If JavaScript tries to manipulate a "wrong" data type, it tries to convert the value to the "right" type.

The results are not always what you expect:

5 + null //returns 5 because null is converted to 0

"5" + null //returns "5null" because null is converted to "null"

"5" + 2 //returns 52 because 2 was converted to "2"

"5" - 2 //returns 3 because "5" is converted to 5

"5" * "2" //returns 10 because "5" and "2" are converted to 5 and 2

Automatic string conversion

JavaScript automatically calls the variable's toString() function when you try to "export" an object or variable:

document.getElementById("demo")[xss_clean] = myVar;

//if myVar = {name:"Fjohn"} // toString is converted to "[object Object]"

//if myVar = [1,2,3,4] // toString converts to "1,2,3,4"

//if myVar = new Date() // toString converts to "Fri Dec 31 2021 11:24:32 GMT+0800"

Numbers and booleans are also converted, but not explicitly:

//if myVar = 123 // toString converts to "123"

//if myVar = true // toString converts to "true"

//if myVar = false // toString converts to "false"

About "JavaScript Boolean conversion values, date conversion to numbers and automatic string conversion methods" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "JavaScript Boolean conversion values, date conversion to numbers and automatic string conversion methods" knowledge. If you still want to learn more knowledge, please pay attention to 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