title: What is a simplest way to add a flag to a number in JavaScript without compromising its functionality?
body: <p>The code below:</p>
  
  <pre><code>var x = 600;
  x.foo = true;
  console.log("value: ",x);
  console.log("foo: ",x.foo);
  </code></pre>
  
  <p>obviously this won't work, as JS numbers are unboxed. An obvious alternative would be to box all your numbers:</p>
  
  <pre><code>var x = { value: 600, foo: true }
  console.log("value: ",x.value);
  console.log("foo: ",x.foo);
  </code></pre>
  
  <p>but suppose you are using this method in a numerically expensive application. Then you get a heavy slowdown, as, now, you are creating a boxed object for every single number in your algorithm, which will wreak havoc in the memory management of the runtime. If all you need is a flag, though, a more lightweight option would be something like:</p>
  
  <pre><code>var x = flag_as_foo(600);
  console.log("value: ",extract_value(x));
  console.log("foo: ",is_foo(x));
  </code></pre>
  
  <p>where <code>flag_as_foo, flag_as_not_foo, is_foo and extract_value</code> could operate by flipping the bits on the number, or maybe using some clever arithmetic. My question is, what is the best way to implement it in JavaScript? This is not obvious, considering JS has no ints and a non-usual way to treat numbers.</p>
  
  <p><strong>Please read the question. I am specifically asking for a bitwise or arithmetic solution of tagging a number.</strong></p>