Home  >  Article  >  Web Front-end  >  Some magical functions of the plus sign ( ) operator in javascript_javascript tips

Some magical functions of the plus sign ( ) operator in javascript_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:45:461513browse

Javascript is a magical language, and there is a magical plus operator in this magical language.

We can use commonly used addition operators:

1. Addition operation, for example: alert(1 2); ==>3
2. String concatenation, for example: alert(“a” ”b”);==>”ab”

The more advanced one is "=", which also performs the above two operations.

I asked a question yesterday in the javascript jungle group: How to convert the date format string "2000-09-11 19:22" into milliseconds?

Mengzhanren answered me immediately every day: new Date('2000-09-11 19:22′). I tried it but it didn’t work. The correct one should be new Date('2000/09/11 19:22′).

The answer seems to be unimportant. You see there is an plus operator in front of it. To be honest, I have never seen this way of writing before. The magical plus operator in JavaScript also has a very magical effect in converting data types, usually between strings and numerical values. For example, the example given by JavaScript Jungle netizen Jason:

Copy code The code is as follows:

// Hexadecimal conversion:
”0xFF ";                                                                                         // -> 255

// Get the current timestamp, equivalent to `new Date().getTime()`:
new Date();

// More secure parsing of strings 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 Boolean type to integer type
true;
//Others:

”1e10″;                                                                -> -12 :




Of course, you can convert numbers into strings by adding numbers to empty strings, for example: alert( typeof (1 ””)); // ->string;

In addition, a subtraction operator is included to convert a string into a number, for example: alert( typeof (“123″-0));//->number;

Of course, there may be some unknown usage features of the plus operator, please leave a message to add!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn