88

Besides :

true ? 1 : 0

is there any short trick which can "translate" True->1 and False->0 in Javascript ?

I've searched but couldn't find any alternative

What do you mean by "short trick" ?

answer : same as ~~6.6 is a trick forMath.floor

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    Chances are you probably don't need to do this. Any arithmetic operation will convert your booleans anyway: eg `true + true + true == 3` – Eric Feb 09 '13 at 11:56
  • 1
    Any operator that converts its operands to numeric values will do. List of all operators: http://es5.github.com/#x11-toc. – Felix Kling Feb 09 '13 at 12:27
  • @FelixKling can you please fix the link ? – Royi Namir Sep 30 '13 at 07:42
  • It should redirect automatically, but here it is: http://es5.github.io/#x11-toc (if that's what you mean). – Felix Kling Sep 30 '13 at 11:05
  • @FelixKling yes for some reason now it is working. before it didnt – Royi Namir Sep 30 '13 at 11:05
  • You need to convert boolean to 1/0 in Javascript to at least use boolean value as a pointer to an array value. For example: arrayOfComboboxValues[valueReturnedByDatabase * 1]. Otherwise you get true or false in the case with combobox, instead of a record. – Zon Jul 07 '14 at 10:51
  • This is all very useful information for cleverly converting, which is fun and possibly useful for competitive programming. However, just want to point out that if you are finding this question as a new developer on a team building software `true ? 1 : 0` is probably what you should be using for maximizing readability. – Tyler Dec 03 '19 at 18:53

5 Answers5

226

Lots of ways to do this

// implicit cast
+true; // 1
+false; // 0
// bit shift by zero
true >>> 0; // 1, right zerofill
false >>> 0; // 0
true << 0; // 1, left
false << 0; // 0
// double bitwise NOT
~~true; // 1
~~false; // 0
// bitwise OR ZERO
true | 0; // 1
false | 0; // 0
// bitwise AND ONE
true & 1; // 1
false & 1; // 0
// bitwise XOR ZERO, you can negate with XOR ONE
true ^ 0; // 1
false ^ 0; // 0
// even PLUS ZERO
true + 0; // 1
false + 0; // 0
// and MULTIPLICATION by ONE
true * 1; // 1
false * 1; // 0

You can also use division by 1, true / 1; // 1, but I'd advise avoiding division where possible.

Furthermore, many of the non-unary operators have an assignment version so if you have a variable you want converted, you can do it very quickly.

You can see a comparison of the different methods with this jsperf.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • can you elaborate on >>> ? – Royi Namir Feb 09 '13 at 12:04
  • 1
    `>>>` is the [_Zero-fill right shift_](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators#>>>_%28Zero-fill_right_shift%29) bitwise operator. `x >>> y` means "move every bit in `x` to the right by `y`, filling the now empty columns with `0`". To choose `y = 0` means you're not moving the bits anywhere, but it still does the cast. – Paul S. Feb 09 '13 at 12:08
  • @RoyiNamir I think you'll find that is mainly due to the age of the machine I'm on. **EDIT** or not! I'm amazed at that. Firefox must have made some serious improvements to it's code. – Paul S. Feb 09 '13 at 12:53
37

Here is a more logical way Number()

Number(true) // = 1
Number(false) // = 0
Muhammad
  • 6,725
  • 5
  • 47
  • 54
  • 4
    Coming from a typed background, I prefer this way because it's explicitly clear about what you're getting back. I look at some of the examples in the accepted answer and I honestly couldn't tell you what would be the result – micnguyen Jul 15 '20 at 15:01
  • 1
    Used this with JQuery to get int value of checkbox and it worked perfectly. Thank you. var chkValue = Number($("#checkboxID").prop("checked")); // = 1 or 0 – Bilbonic Feb 01 '21 at 21:59
13

...or you can use +true and +false

bsiamionau
  • 8,099
  • 4
  • 46
  • 73
12

You can use ~~boolean, where boolean is (obviously) a boolean.

~~true  // 1
~~false // 0
whirlwin
  • 16,044
  • 17
  • 67
  • 98
1

To convert default switch value from boolean to numeric 0 or 1 to match your backend value.


0 should be converted into "false"

1 should be converted into "true"

This can be done using logic:

!!0 => false
!!1 => true
jhon26
  • 313
  • 2
  • 11
  • 1
    I do not understand your explanation, half of which seems to be non-English. The code looks like it has the question in reverse, turning 0 into false and 1 into true. Please [edit] and try to improve, ideally according to [answer]. – Yunnosch Jan 03 '23 at 12:36