title: Why is my Javascript view on a buffer all zeroes?
body: <p>I create a buffer and then a Uint8Array on it, but the array does not have any values. I would expect it to have the values of the buffer. This is an easily reproducible example:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var buf = new ArrayBuffer(32);
  for (var index = 0; index &lt; 32; index++) buf[index] = index;
  console.log(buf);
  var arr = new Uint8Array(buf);
  console.log(arr);</code></pre>
  </div>
  </div>
  </p>
  
  <p>The thing I tried in reality is a date format converter like this:</p>
  
  <pre><code>//Buffers and views
  function convertDateTimeToFormat(date, initialFormat, endFormat) {
      var buf = new ArrayBuffer(14);
      var result = new Uint8Array(buf);
      console.log(date);
      var initialPositions = {};
      var endPositions = {};
      var sizeSoFar = 0;
      for (var c of initialFormat) {
          if (c === 'y') {
              initialPositions.y = new Uint32Array(date, sizeSoFar, 1);
          } else {
              initialPositions[c] = new Uint8Array(date, sizeSoFar, 2);
          }
          sizeSoFar += ((c === 'y') ? 4 : 2);
      }
      sizeSoFar = 0;
      for (var c of endFormat) {
          if (c === 'y') {
              endPositions.y = new Uint32Array(buf, sizeSoFar, 1);
          } else {
              endPositions[c] = new Uint8Array(buf, sizeSoFar, 2);
          }
          sizeSoFar += ((c === 'y') ? 4 : 2);
      }
      for (var key in initialPositions) {
          var limit = (key === 'y') ? 4 : 2;
          for (var index = 0; index &lt; limit; index++) endPositions[c][index] = initialPositions[c][index];
      }
      return result;
  }
  //2019-03-01 13:03:50
  var buf = new ArrayBuffer( 14 );
  buf[0] = 2;
  buf[1] = 0;
  buf[2] = 1;
  buf[3] = 9;
  buf[4] = 0;
  buf[5] = 3;
  buf[6] = 0;
  buf[7] = 1;
  buf[8] = 1;
  buf[9] = 3;
  buf[10] = 0;
  buf[11] = 3
  buf[12] = 5;
  buf[13] = 0;
  console.log(convertDateTimeToFormat(buf, "yMdHms", "yMdHms"));
  console.log(convertDateTimeToFormat(buf, "yMdHms", "MdyHms"));
  </code></pre>
  
  <p>But due to the behavior I described at the start of this question, the results are all zeroes.</p>
  
  <p>This works but it's not elegant, because it expects a date format and if I am to ensure that the input is agnostic to date formats, then the code will become very complicated:</p>
  
  <pre><code>//Buffers and views
  var results = {};
  var buf = new ArrayBuffer( 4 );
  results.uint32 = new Uint32Array(buf);
  results.int8 = new Uint8Array(buf);
  results.uint8 = new Int8Array(buf);
  results.int8[2] = -1;
  console.log(results);
  results.int8[2] = 0;
  results.int8[1] = -1;
  console.log(results);
  results.int8[1] = 0;
  results.int8[0] = -1;
  console.log(results);
  
  //Buffers and views
  function convertDateTimeToFormat(date, format) {
      var buf = new ArrayBuffer(14);
      var result = new Uint8Array(buf);
      var positions = {
          y: 0,
          M: 4,
          d: 6,
          H: 8,
          m: 10,
          s: 12
      };
      for (var index = 0; index &lt; 14; index++) {
          result[index] = date[positions[format[index]]++];
      }
      return result.join("");
  }
  var results = {};
  //2019-03-01 13:03:50
  var buf = new ArrayBuffer( 14 );
  buf[0] = 2;
  buf[1] = 0;
  buf[2] = 1;
  buf[3] = 9;
  buf[4] = 0;
  buf[5] = 3;
  buf[6] = 0;
  buf[7] = 1;
  buf[8] = 1;
  buf[9] = 3;
  buf[10] = 0;
  buf[11] = 3
  buf[12] = 5;
  buf[13] = 0;
  console.log(convertDateTimeToFormat(buf, "yyyyMMddHHmmss"));
  console.log(convertDateTimeToFormat(buf, "MMddyyyyHHmmss"));
  </code></pre>