2

They both seem to output the same results and turn strings into numbers. Is there a difference I am not aware about? I can't seem to find any documentation regarding ~~ operator.

var hey = true
hey = +hey //hey = 1

var hey = true 
hey = ~~hey //hey = 1

var num = "1231"
num = ~~num //num = 1231

var num = "1231"
num = +num //num = 1231

There is one difference that I found and that's ~~ will always try to output a number whereas there are cases for + to simply return NaN

num = "omfg"
num = ~~num //num = 0

num = "omfg"
num = +num //num = NaN

num = {}
num = ~~num //num = 0

num = {}
num = +num //num = NaN

Any clarification would be awesome :)

aug
  • 9,482
  • 6
  • 63
  • 84
  • 1
    The tilde is a bitwise **not**, repeating it twice is like a bitwise `!!`, only it turns the bits one by one. – adeneo Feb 04 '13 at 19:12
  • As a side note, I'd consider both of these bad practice; you should be using `parseInt` or `parseFloat` for converting strings to numbers. – jbabey Feb 04 '13 at 19:20
  • @jbabey thanks for clarifying that. If anyone is curious, I was wondering because of this question http://stackoverflow.com/questions/6911235/is-there-a-better-way-of-writing-v-v-0-1-0 – aug Feb 04 '13 at 21:44

3 Answers3

5

Both will implicitly turn the operand into a number, because the operators can only be used on a number.

The difference is that the ~ operator is a bitwise operator, so it will also turn the number into a 32 bit integer. (The result will still be of the type Number though, i.e. a double precision floating point number.)

Neither is a descriptive way to turn a value into a number, as they both use a side effect of the actual operation. Normally you would use a function like parseInt or parseFloat to convert a string to a number.

Guffa
  • 640,220
  • 96
  • 678
  • 956
2

Yes, there is a difference. Try ~~"3.4".

Just like +, ~~ converts, whenever possible, what follows to a number but unlike + it makes it an integer.

Bitwise operators always reduce numbers to 32 bits integers in javascript. And this one "Inverts the bits of its operand" (which means the integer part won't be changed by a double execution).

From the MDN :

The operands of all bitwise operators are converted to signed 32-bit integers in big-endian order and in two's complement format.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
2

+num will convert a string to a number, but doesn't convert decimal numbers to integers. For strings that contain characters as well as numbers, NaN is returned.

~~num will convert the string to a number and cuts off any decimals (without rounding). For strings that contain characters as well as numbers, 0 is returned.

Elliot Bonneville
  • 46,945
  • 19
  • 86
  • 120
  • +1, also note that the rounding is a bit special: for negative values, it is `ceil` but for positive numbers it is `floor`. – pimvdb Feb 04 '13 at 19:16
  • Ah, is that the case? Editing to correct. -Edit- @pimvdb: that doesn't seem to be the case, as `~~"-3.4"` returns -3. – Elliot Bonneville Feb 04 '13 at 19:17
  • What I mean is that `~~"3.4"` returns `3` and `~~"-3.4"` returns `-3`. The former is analogous to `Math.floor`, but the second to `Math.ceil`. – pimvdb Feb 04 '13 at 19:25
  • Oh, right, that makes sense now. I dunno how I missed that--must have been thinking too hard, or perhaps not hard enough. – Elliot Bonneville Feb 04 '13 at 19:26