• Node.js Video Tutorials

Node.js - os.endianness() Method



The Node.js os.endianness() method will return a string, that will let us know the endianness of the CPU for which the Node.js binary was compiled.

The word endianness is the order or sequence of bytes of a word of digital data in computer memory. The endianness is primarily expressed as big-endian(BE) or little-endian(LE).

Syntax

Following is the syntax of the Node.js os.endianness() method −

os.endianness()

Parameters

This method does not accept any parameters.

Return value

This method will return a string value that specifies the endianness of the current computer. The string can return either big-endian(BE) or little-endian(LE).

  • Little-endian(LE) − This is an order in which the "little end" (least significant value in the sequence) will be stored first.

  • Big-endian(BE) − This is an order in which the "big end" (most significant value in the sequence) will be stored first, at the lowest memory address.

Example

In the following example, we are trying to log the Node.js os.endianness() method to the console for printing the endianness of the current system.

const os = require('os');
const {endianness} = os;
console.log(os.endianness());

Output

If we compile and run the above program, the os.endianness() method returns the string value that specifies the endianness of the current computer.

LE

Example

In the example below,

  • We are performing a switch case.

  • So the switch will check each case against the output string value of the os.endianness() method until a match is found.

  • If nothing matches, the default condition will be printed.

const os = require('os');
  
let end = os.endianness();
switch(end) {
   case 'LE':
      console.log("CPU is little endian(LE) format");
      break;
         
   case 'BE':
      console.log("CPU is big endian(BE) format");
      break;
      
   default:
      colsole.log("Unknown endianness");
}

Output

When we compile and run the above program, the output string value of os.endianness() method will be 'LE'. So the case 'LE' got matched and it got executed.

CPU is little endian(LE) format
nodejs_os_module.htm
Advertisements