title: Does anyone know how to count the number of bytes in a string?
body: <p>If I have a string in JavaScript what's the best way to go about counting the number of bytes or bits the string takes up?</p>
  
  <p>My first thought is that, since the EcmaScript spec (5.1) states in 8.4 that string elements in EcmaScript consist of 16 bits values, and that the languages uses UTF-16 character encoding, I should get the Decimal representation of that (charCodeAt), divide that by two, floor the result, and Bob's your father's brother.</p>
  
  <p>But if I were to convert each character to binary (toString(2)), in order to count the bits, do I count the 0's as well as the 1's? Or just the 1's?</p>
  
  <p>From what I've read online (and the information is sparse), I'm thinking it's something like:</p>
  
  <pre><code>Math.floor("A".charCodeAt(0) / 2); // 32 bits... No?
  </code></pre>