Skip to content

Instantly share code, notes, and snippets.

@jonasraoni
Created October 29, 2020 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasraoni/a7bd7c5ca328543d88afa94e9ef2d184 to your computer and use it in GitHub Desktop.
Save jonasraoni/a7bd7c5ca328543d88afa94e9ef2d184 to your computer and use it in GitHub Desktop.
Convert boolean to positive (1) negative (-1)
const condition = true;
// I don't know why, but adding an if here makes me sad since I started programming :L
condition ? 1 : -1;
// Alternative way 1
(-1) ** condition;
// Alternative way 2
2 * condition - 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment