91

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

7
  • 2
    Chances are you probably don't need to do this. Any arithmetic operation will convert your booleans anyway: eg true + true + true == 3 Commented Feb 9, 2013 at 11:56
  • 1
    Any operator that converts its operands to numeric values will do. List of all operators: es5.github.com/#x11-toc. Commented Feb 9, 2013 at 12:27
  • @FelixKling can you please fix the link ? Commented Sep 30, 2013 at 7:42
  • It should redirect automatically, but here it is: es5.github.io/#x11-toc (if that's what you mean). Commented Sep 30, 2013 at 11:05
  • @FelixKling yes for some reason now it is working. before it didnt Commented Sep 30, 2013 at 11:05

5 Answers 5

232

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.

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

3 Comments

can you elaborate on >>> ?
>>> is the Zero-fill right shift 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.
@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.
42

Here is a more logical way Number()

Number(true) // = 1
Number(false) // = 0

2 Comments

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 😅
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
14

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

Comments

12

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

~~true  // 1
~~false // 0

Comments

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

1 Comment

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 How to Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.