0

The following code is from https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encodeInto under heading "Encode Into A Specific Position".

var encoder = new TextEncoder;
function encodeIntoAtPosition(string, u8array, position) {
    return encoder.encodeInto(string, position ? u8array.subarray(position|0) : u8array);
}

var u8array = new Uint8Array(8);
encodeIntoAtPosition("hello", u8array, 2);
console.log( "" + u8array.join() ); // 0,0,104,101,108,108,111,0

In encodeIntoAPosition, what is the reason for position|0?

Here are some results for bitwise OR operations.

console.info([ 2|0, 2.5|0, 'z'|0, 'abc'|0, -2|0, -2.5|0 ]);

Thanks.

Logan Lee
  • 807
  • 9
  • 21

0 Answers0