Home >Web Front-end >JS Tutorial >Some magical functions of the plus sign ( ) operator in javascript_javascript tips
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:
// 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:
Of course, you can convert numbers into strings by adding numbers to empty strings, for example: alert( typeof (1 ””)); // ->string;
Of course, there may be some unknown usage features of the plus operator, please leave a message to add!