The magic (+) plus operator in javascript

Source: Internet
Author: User

Javascript is a magic language, but there is no magic plus operator in it.

Common addition operators can be used:

  1. Addition operation, such as alert (1 + 2) ;=> 3
  2. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.