Javascript is a magic language, but there is no magic plus operator in it.
Common addition operators can be used:
- Addition operation, such as alert (1 + 2) ;=> 3
- String connection, for example, alert ("a" + "B"); => "AB"
"+ =" Is also advanced, and the above two operations are also done.
Yesterday, I asked a question in the javascript jungle group: How can I convert the Date Format String "" into milliseconds?
Mengren answered me every day: + new Date ('2017-09-11 '). I tried it, + new Date ('2017/11 ') is correct ′).
The answer does not seem to matter anymore. You can see that there is a plus operator before. To tell the truth, this method has never been seen before. The addition operator in the magic javascript has a very magical effect. The conversion of data types is generally the conversion of strings and values. For example, the example given by jason, a javascript jungle netizen:
// Hexadecimal conversion:
+ "0xFF"; //-> 255
// Obtain the current timestamp, which is equivalent to 'new Date (). getTime ()':
+ New Date ();
// A safer string to parse than parseFloat ()/parseInt ()
ParseInt ("1,000"); //-> 1, not 1000
+ "1,000"; //-> NaN, much better for testing user input
ParseInt ("010"); //> 8, because of the octal literal prefix
+ "010"; //-> 10, 'Number () 'doesn' t parse octal literals
// Some simple abbreviations such as: if (someVar = null) {someVar = 0 };
+ Null; //-> 0;
// Convert the boolean type to an integer type
+ True; //-> 1;
+ False; //-> 0;
// Others:
+ "1e10"; //-> 10000000000
+ "1e-4"; //-> 0.0001
+ "-12"; //->-12:
Of course, you can convert a number into a string by adding a number to a null string, for example, alert (typeof (1 + ""); //-> string;
In addition, a subtraction operator is provided to convert a string to a number, for example, alert (typeof ("123"-0); //-> number;
Of course, there may be some unknown features for adding operators. Please leave a message to add them! Thank you, Tom. Thank you, jason. Thank you to other netizens in the javascript jungle.
(Source: Web Front-end development, original: http://www.css88.com/archives/4343)