how to check local endianness with javascript 3f

Solutions on MaxInterview for how to check local endianness with javascript 3f by the best coders in the world

showing results for - "how to check local endianness with javascript 3f"
Gaia
31 Nov 2019
1let localEndianness = () => {
2    let uInt32 = new Uint32Array([0x12345678]); // have a 4 bytes long thing
3    let uInt8 = new Uint8Array(uInt32.buffer); // split it in 1 byte long things
4 	// Now, uInt8[0] returns the first byte of uInt32 according to the machine running this program. 
5    if(uInt8[0] === 0x78) { // here we got the byte of lesser order
6        return 'little';
7    } else if (uInt8[0] === 0x12) { // here we got the byte of bigger order
8        return 'big'; 
9    } else { // you may check for older or stranger behaviours here, and even endianness on bits rather than bytes if needed. But hey, ECMAScript languages usually don't run on such rare machines.  
10        return 'mixed'; // mixed yes, but how mixed ?
11    }
12};