In node. JS source code, it is common to use various symbols to process strings as numbers. May be written by different people, the use of different styles. Basically there are some of the following.
Convert a string to a number +
A simple way to convert a number string into a number is to prefix it with a number +
.
VarSi= + ";//12 var SN = + ' -12 ' ;// -12 var SF = + ' 12.7 ' ;//12.7 var SS = + ' 12s ' ;//NaN
*
To convert using multiplication symbols *
:
VarSi= ' 12 ' * 1; 12 Varsn= ' -12 ' * 1; // -12 var sf = ' 12.7 ' * 1; //12.7 var ss = ' 12s ' * 1; //NaN
Convert a string to an integer ~ ~
The operator originally intended to be bitwise ~
reversed, using the ability to convert ~~
a string that can be converted to a number into an integer:
VarSi= ~~' 12 '; 12 Varsn= ~~ ' -12 ' -12 var SF = ~~ ' 12.7 ' ;//12 var SNF = ~~ ' -12.7 ' ;// -12 var SS = ~~ ' 12s ' ;//0
>>
The operator is intended to be >>
a signed right shift, using the ability to convert >> 0
a converted string to an integer:
VarSi= ' 12 ' >> 0; 12 Varsn= '-12 ' >> 0; -12 Varsf= ' 12.7 ' >> 0;//12 var SNF = ' -12.7 ' >>0; // -12 var SS = ' 12s ' >>0; //0
>>>
The operator is intended to >>>
move right without a symbol, and you can use to >>> 0
convert a string to an integer:
VarSi= ' 12 ' >>> 0; 12 Varsn= ' -12 ' >>> Span class= "lit" >0; //4294967284 var SF =< Span class= "PLN" > 12.7 ' >>> Span class= "lit" >0; //12 var SS = ' 12s ' >>>0; //0
Attention
When using the above symbol, its conversion is sometimes different from the library that comes with JavaScript. Such as:
VarSfp= ' 12.4 '; VarSfn= '-12.7 '; VarMsfp= Math.Floor(Sfp //12 var NSFP = ~~sfp; Span class= "com" >//12 var MSFN = math.sfn// -13 var NSFN = ~~sfn;// -12
There are several scenarios for using symbols instead of JavaScript libraries:
- The use of symbols may be better than the performance of library functions. However, due to the different JS interpreter, it is possible to reverse the situation.
- Reduce the transmission of characters, the use of symbols will save a certain amount of space.
At some point, however, you must use library functions. such as: parseInt(‘12s‘)
.
JS operator Conversion number