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 convert data types by javascript

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

Share

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

This article will explain in detail how to convert javascript to data types. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The method of javascript conversion data type: 1, automatically convert the data type of value according to the operation environment to meet the needs of operation; 2, use JavaScript built-in functions such as toString (), String (), parseInt () to force the conversion of data type.

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

JavaScript is a dynamic language, and the so-called dynamic language can be understood temporarily as everything in the language is uncertain. For example, a variable, which is an integer at this moment, may become a string the next. Although the data type of a variable is uncertain, various operators have requirements for the data type. If the operator finds that the type of the operator does not match the expectation, the type is automatically converted.

To put it simply, JavaScript can automatically convert the types of values according to the computing environment to meet the needs of the operation.

Example: use the plus sign operator to convert a value to a string

/ / convert the number to the string var n = 123 X n = n + "; console.log (typeof n); / / return type is string

Example: use the multiplication operator to convert a string into a numeric value

Var n = "123"; n = n * 1console.log (typeof n); / / return type is number

However, in many cases, developers are required to manually convert data types (cast) to control the operation process.

1. Convert other data types to String

Method 1: toString () method

Call the toString () method of the converted data type, which does not affect the original variable and returns the result of the conversion, but note: the values null and undefined do not have toString, and an error will be reported if their method is called.

Var a = 123a.toString () / / "123" var b = null;b.toString () / / "error report" var c = undefinedc.toString () / / "error report"

Using the base mode of the toString () method of type Number, you can output numbers on different bases, such as 2 for binary, 8 for octal, and 16 for hexadecimal.

Var iNum = 10 alert (iNum.toString (2)); / output "1010" alert (iNum.toString (8)); / output "12" alert (iNum.toString (16)); / / output "A"

Method 2: String () function

When you use the String () function to cast a type, it is actually a called toString () method for Number and Boolean.

But for null and undefined, the toString () method is not called, it converts null directly to "null" and undefined directly to "undefined"

Var a = nullString (a) / / "null" var b = undefinedString (b) / / "undefined"

If the parameter of the String method is an object, it returns a type string; if it is an array, it returns the string form of the array.

String ({a: 1}) / / "[object Object]" String ([1min2 object Object 3]) / / "1Jing 2 Magi 3"

2. Convert other data types to Number

Method 1: use the Number () function

The following is divided into two cases, one is that the parameter is the value of the original type, and the other is that the parameter is the object.

(1) original type value

① string to digit

If it is a pure numeric string, convert it directly to a number

If there is non-numeric content in the string, it is converted to NaN

If the string is an empty string or a string full of spaces, convert to 0

Number ('324') / / 324Number (' 324abc') / / NaNNumber ('') / / 0

② Boolean value to number: true to 1pm false to 0

Number (true) / / 1Number (false) / / 0

③ undefined to digital: to NaN

Number (undefined) / / NaN

④ null to number: to 0

Number (null) / / 0

⑤ Number () takes a numeric value as a parameter, so it can recognize both negative hexadecimal and octal with zero, and the return value is always decimal.

Number; / / 3.15Number (023); / / 19Number (0x12); / / 18Number (- 0x12); / /-18

(2) object

The simple rule is that when the argument to the Number method is an object, it returns NaN, unless it is an array containing a single numeric value.

Number ({a: 1}) / / NaNNumber ([1,2,3]) / / NaNNumber ([5]) / / 5

Method 2: parseInt () & parseFloat ()

This method is specifically used to deal with strings. ParseInt () converts a string to an integer, which takes the contents of a valid integer out of a string and converts it to Number. ParseFloat () converts a string to a floating point number. The effect of parseFloat () is similar to that of parseInt (), except that it can obtain valid decimals.

Console.log (parseInt ('.21')); / / NaNconsole.log (parseInt (' 10.3')); / / 10console.log (parseFloat ('.21')); / / 0.21console.log (parseFloat (' .d1')); / / NaNconsole.log (parseFloat ("10.11.33")); / / 10.11console.log (parseFloat ("4.3years")); / / 4.3console.log (parseFloat ("He40.3")) / / NaN

ParseInt () defaults to the decimal conversion value when there is no second parameter, converts the value with the second parameter as the cardinality, and returns NaN if the cardinality is incorrect.

Console.log (parseInt ("13")); / / 13console.log (parseInt ("11", 2)); / / 3console.log (parseInt ("17", 8)); / / 15console.log (parseInt ("1f", 16)); / / 31

The difference between the two: the Number function converts a string to a numeric value, which is much stricter than the parseInt function. Basically, as long as one character cannot be converted to a numeric value, the entire string is converted to NaN.

ParseInt ('42 cats') / / 42Number ('42 cats') / / NaN

In the above code, parseInt parses the characters one by one, while the Number function converts the type of the string as a whole.

In addition, empty strings are handled differently.

Number ("); / / 0 parseInt ("); / / NaN

3. Convert other data types to Boolean

Its conversion rules are relatively simple: only empty strings (""), null, undefined, + 0,-0 and NaN convert to Boolean false, others are true, empty array and empty object conversion to Boolean type is also true, and even the Boolean object new Boolean (false) corresponding to false is true.

Boolean (undefined) / / falseBoolean (null) / / falseBoolean (0) / / falseBoolean (NaN) / / falseBoolean (') / / falseBoolean ({}) / / trueBoolean ([]) / / trueBoolean (new Boolean (false)) / / true this is the end of the article on "how to convert javascript to data types". I hope the above can be helpful to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.

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