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 javascript converts types

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

Share

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

Editor to share with you how to change the type of javascript, I hope you will learn something after reading this article, let's discuss it together!

Javascript type conversion methods: 1, using operators such as "= =" and "+" for implicit type conversion; 2, using type conversion functions such as Boolean (), Number (), String (), Object (), toString (), toFixed () for explicit type conversion.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

1. Implicit type conversion

① = if the two variables of the equality operation are not of the same type, the equality operator attempts some type conversion:

If one is null and the other is undefined, then they are equal.

If one value is a number and the other is a string, convert the string to a number first, and then use the converted value for comparison

If one is true, convert it to 1 and then compare. If one of them is false, convert it to 0 and compare again.

If one value is an object and the other is a number or string, the object is converted to the original value and compared. The object is converted to the original value through the toString () or valueOf () method. The built-in class at the core of the JS language first tries to use valueOf (), and then tries to use toString (). Except for the date class, the date class uses only the toString () transformation.

Other different types of comparisons are not equal.

two

< >

= comparison operator. Operands can be of any type, but only numbers and strings can actually perform comparison operations.

If the Operand is an object, the object is converted to the original value first: if valueOf () returns an original value, then the original value is used directly. Otherwise, use toString () conversion

After the object is converted to the original value, if both operands are strings, compare them according to strings

After the object is converted to the original value, if at least one Operand is not a string, both are converted to numbers for comparison. If one of them is NaN, the result is false.

The ③-minus operator converts both operands to numbers.

④ + plus operator, if one Operand is a string, then the other Operand is also converted to a string. One dollar plus converts its Operand to a number.

⑤! converts to a Boolean value and inverts it. !! X is equivalent to Boolean (x).

2. Explicit type conversion

① uses the Boolean () / Number () / String () / Object () function. Any value except null and undefined has a toString () method, and the execution result of this method is usually consistent with the return result of the String () method.

② digit to string

The toString () method, which accepts parameters and converts the comparison to decimal.

ToFixed (), the parameter indicates that several decimal places are retained.

ToExponential (), the parameter indicates that several decimal places are reserved.

ToPrecision (), the parameter represents the number of significant digits.

③ string to digit

ParseInt () function

The parseFloat () function is a global function, not a method of any class.

The ④ object is converted to the original value

The toString () method.

The array class converts each element to a string and adds a comma composite string between the elements

The function class returns the representation of the implementation definition of this function.

The date class returns a readable date and time string.

The RegExp class is converted to a direct literal.

The valueOf () method. If an object has any original value, it converts the object to the original value that represents it by default. But the object is a compound value, and in most cases valueOf () returns the object itself.

The object-to-string conversion starts with the toString () method, and the object-to-number conversion starts with the valueOf () method.

After reading this article, I believe you have a certain understanding of "how to change types of javascript". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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