In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 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 data types in javascript. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Javascript data type conversion includes explicit type conversion and implicit type conversion. Explicit type conversions are mainly done by using JavaScript built-in functions, while implicit type conversions are types that JavaScript automatically converts values according to the computing environment.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Javascript data type conversion includes explicit type conversion and implicit type conversion.
Explicit conversion of data types
The conversion data types displayed are mainly through the data conversion methods defined by JS.
1. Convert to a string
Most JavaScript hosting environments (such as Node.js and Chrome) provide the global function toString; while Object.prototype defines the toString method so that all objects have the ability to convert to strings.
For example, a Number is converted to String:
Var n = 1 × n.toString (); / /'1'
ToString accepts a parameter-specified base, which defaults to 10. 0. You can use this parameter to generate random strings that include letters and numbers:
Math.random () .toString (36) .substr (2)
Random generates a random number from 0 to 1, the 36-ary character set is [0-9a-z] (36), and substr is used to truncate the initial "0." In addition, Object.prototype.toString can be used to detect the type of JavaScript object:
Var toString = Object.prototype.toString;toString.call (new Date); / / [object Date] toString.call (new String); / / [object String] toString.call (Math); / / [object Math] / / Since JavaScript 1.8.5toString.call (undefined); / / [object Undefined] toString.call (null); / / [object Null] / / Custom type toString.call (new MyClass); / / [object Object]
2. Convert to a number
Converting strings to numbers is also a common requirement, which is usually used to get a Number from user input or files, and you can use parseInt and parseFloat directly in JavaScript. For example:
Var iNum1 = parseInt ("12345red"); / return 12345var iNum1 = parseInt ("0xA"); / / return 10var iNum1 = parseInt ("56.9"); / / return 56var iNum1 = parseInt ("red"); / / return NaNvar fNum4 = parseFloat ("11.22.33"); / / return 11.22
Note that NaN is the only value in JavaScript that is not equal to itself. (NaN = = NaN) = false! If illegal characters are encountered, parseInt and parseFloat will ignore everything after that.
ParseFloat only accepts a string of decimal numbers, while parseInt also provides a second parameter (optional) to specify the base in which the string represents the number:
Var iNum1 = parseInt ("10", 2); / return 2var iNum2 = parseInt ("10", 8); / / return 8var iNum3 = parseInt ("10", 10); / / return 10
3. Forced type conversion
Boolean (0) / / = > false-zero Boolean (new object ()) / / = > true-object Number (undefined) / / = > NaNNumber (null) / / = > 0String (null) / / = > "null"
Implicit data conversion
Automatically converted by JavaScript itself. JavaScript can automatically convert the types of values according to the computing environment to meet the needs of the operation.
1. Increment and decrement operators
The increment and decrement operators are borrowed directly from C, and there are two versions each: pre-type and post-type (asides +, amura -, + a,-a). As the name implies, the pre-type should precede the variable to be manipulated, and the post-type should precede the variable to be manipulated.
These four operators apply to any value, that is, they apply not only to integers, but also to strings, Boolean values, floating-point values, and objects, accompanied by implicit data type conversions.
2. Four operations of one yuan
The addition operator + is a binocular operator, and as long as one of them is of type String, the value of the expression is a String.
For the other four operations, only one of them is of type Number, and the value of the expression is a Number.
NaN is usually returned for illegal characters:
'1' *' a'/ / = > NaN, because the parseInt (a) value is NaN,1 * NaN or NaN
3. Logical non-operator and comparison operator convert any value to a Boolean value
A logical non-operator first converts its Operand to a Boolean value, and then inverts it.
4. Relational comparison operator
5. Judgment sentence
The judgment condition in the judgment statement needs to be of type Boolean, so the conditional expression is implicitly converted to Boolean. The conversion rules are the same as the constructor of Boolean. For example:
Var obj = {}; if (obj) {while (obj);}
6. Native code call
JavaScript hosting environments provide a large number of objects, often many of which are implemented through JavaScript. The parameters passed in by JavaScript to these functions are also implicitly converted. For example, the alert method provided by BOM accepts parameters of type String:
Alert ({a: 1}); / / = > [object Object] so much about how javascript converts data types. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.