Many friends are familiar with the double vertical Bar "| |", because this is often used. But do you know the single vertical bar?
Read JavaScript practical tips, JS small knowledge article, saw a single vertical bar "|" Operation, which is unfamiliar to it.
Learn and master it.
JS operator single vertical bar "|" The role
In JS operation, number | When you are 0:
A. Integer operation, the equivalent of removing the decimal point, parseint.
B. When positive, it is equivalent to rounding down, Math.floor ().
C. When negative, it is equivalent to rounding up, Math.ceil ().
// Rounding up // Rounding down // Rounding Rounded rounding Console.log (// 0// 1// 3// -5 // -7
The methods used to deal with numbers are: parseint (), parsefloat (), toFixed (), toprecision ()
ToFixed (), leaving several decimals, rounded, and the result is a string.
// "100.46" // "100.456" // "100.46"
Toprecision (), preserving several numbers, rounding, and the result is a string.
// "99.456" // "100.46" // "10.456"
Single Vertical Bar "|" The arithmetic rules
"Number|0" can achieve the purpose of rounding, if the single vertical bar is not 0, the result will be how much?
// 7 // 4 // One // 5 // 3455
It seems that there is no rule to follow. Not really, single vertical bar "|" Is the result of the addition of the converted to 2 binary.
// 7 // after conversion to binary 011|110 add to get 111=7console.log (// 4// convert to binary after 100|100 add to get 100=4 Console.log (// /// convert to binary after 1000|011 add to get 1011=11 Console.log (/// 5// convert to binary after 101|100 add to get 101=5
JS operator single vertical bar "|" The usage and function of JS data processing