2

I have this line that I copied from another place:

Total += parseFloat($(this).val())|0;

What's the function of the operator |? When I change the number, I get different results.

smholloway
  • 589
  • 7
  • 14
Nicolas400
  • 451
  • 1
  • 6
  • 21
  • 4
    MDN has a JavaScript reference: https://developer.mozilla.org/en/JavaScript/Reference#Operators_and_other_keywords – Felix Kling Feb 27 '12 at 22:00
  • hi, thanks for all te answers, just to note, when the input field value was "3.5" the funcion return "3", when I change "0" and put "2", i get "5" ... in total variable... I assume it was som position related parameter ... – Nicolas400 Feb 27 '12 at 22:34
  • 1
    @Nicolas400: Someone upvoted my answer (the accepted answer), which made me look at it to remind myself what it was, and I was unhappy to find it was *wrong*. It said `|0` would be like `Math.floor`, which is only true for positive numbers. Fixed, FYI. – T.J. Crowder Feb 13 '14 at 15:27

2 Answers2

13

The | in JavaScript is an integer bitwise OR operator. In that context, it strips off any fractional portion returned by parseFloat. The expression parseFloat($(this).val()) will result in a number with (potentially) a fractional component, but then |0 will convert it to an integer number, OR it with 0 (which means it won't change), and so the overall result is to get a whole number.

So functionally, it truncates the fractional portion off the number. -1.5 becomes -1, and 1.5 becomes 1. This is like Math.floor, but truncating rather than rounding "down" (Math.floor(-1.5) is -2 — the next lowest whole number — rather than -1 as the |0 version gives us).

So perhaps that's why it was used, to chop off (rather than "floor") the fractional portion of the number.

Alternately, it could be a typo. The author of that code might have meant to write this (note || rather than |):

Total += parseFloat($(this).val()) || 0;

That defends against the possibility that $(this).val() returns "" or similar, resulting in parseFloat returning NaN. It uses the curiously-powerful || operator to return 0 rather than NaN in that case. (And there's an advertisement for putting spaces around your operators.) Would have to know the context of the code to say whether truncating to a whole number (|) makes sense when adding to Total, or if they were just defending the NaN case.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • What are some instances where it is helpful to use bitwise operators? – Jasper Feb 27 '12 at 22:03
  • @Jasper: There are all sorts of times you want bitwise operators, this is just an unusual use of one. – T.J. Crowder Feb 27 '12 at 22:04
  • It also converts `NaN` to `0`, and makes sure the `.val()` is evaluated as a decimal, where `parseInt` would need the extra radix arg. –  Feb 27 '12 at 22:14
  • 1
    @amnotiam: LOL, I was just editing my answer to mention `NaN`, but in relation to `||` rather than `|`. – T.J. Crowder Feb 27 '12 at 22:20
3

The | operator in javascript is the bitwise or operator

This operator treats the operands as 32 bit integers and for every bit returns 1 if either is 1 and 0 otherwise.

JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • How it convert the operands to 32 bit integers? – Shiplu Mokaddim Feb 27 '12 at 22:02
  • 1
    @Shiplu: It's defined in the specification: http://es5.github.com/#x9.5 (which does not mean that browsers really do it that way). – Felix Kling Feb 27 '12 at 22:03
  • @Shiplu i'm not exactly sure how it does that. Likely converts them first to numbers and then rounds to 32 bit integers. – JaredPar Feb 27 '12 at 22:03
  • 2
    @Shiplu: Yes, it converts it to a 32-bit integer (http://es5.github.com/#x9.5) -- temporarily, for the calculation. Then the 32-bit integer is converted back to an IEEE 64-bit float (the only kind of numbers that JavaScript has, except for temporary integers during calculations like this one), but at that point the fractional part has been removed (e.g., `Math.floor`). – T.J. Crowder Feb 27 '12 at 22:11
  • Thanks for the explanation. I also though the result is float. – Shiplu Mokaddim Feb 27 '12 at 22:46