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.

3
  • 4
    MDN has a JavaScript reference: developer.mozilla.org/en/JavaScript/… Commented Feb 27, 2012 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 ... Commented Feb 27, 2012 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. Commented Feb 13, 2014 at 15:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What are some instances where it is helpful to use bitwise operators?
@Jasper: There are all sorts of times you want bitwise operators, this is just an unusual use of one.
It also converts NaN to 0, and makes sure the .val() is evaluated as a decimal, where parseInt would need the extra radix arg.
@amnotiam: LOL, I was just editing my answer to mention NaN, but in relation to || rather than |.
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.

5 Comments

How it convert the operands to 32 bit integers?
@Shiplu: It's defined in the specification: es5.github.com/#x9.5 (which does not mean that browsers really do it that way).
@Shiplu i'm not exactly sure how it does that. Likely converts them first to numbers and then rounds to 32 bit integers.
@Shiplu: Yes, it converts it to a 32-bit integer (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).
Thanks for the explanation. I also though the result is float.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.