Node.js | os.endianness() Method

Endianness refers to the order of bits in a sequence within a binary representation of a number.

The os.endianness() method is an inbuilt application programming interface of the os module which is used to get endianness of the CPU of the computer for which the node.js is compiled.

Syntax:



os.endianness()

Parameters: This method does not accept any parameters.

Return Value: This method returns a string value that specifies the endianness of the CPU. The returned string will be either BE (for big endian) or LE (for little endian).

  • LE: It is the most significant bits/values in the sequence that is stored in higher memory address.
  • BE: It is the most significant bits/values in the sequence that is stored in lower memory address.

Below examples illustrate the use of os.endianness() method in Node.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// os.endianness() method
  
// Allocating os module
const os = require('os');
  
// Printing os.endianness() value
console.log(os.endianness());

chevron_right


Output:

LE

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// os.endianness() method
  
// Allocating os module
const os = require('os');
  
// Printing os.endianness() value
switch(os.endianness()) {
    case 'LE':
      console.log("CPU is little endian format");
      break;
      
    case 'BE':
      console.log("CPU is big endian format");
      break;
      
    default:
      colsole.log("Unknown endianness");
}

chevron_right


Output:

CPU is little endian format

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/os.html#os_os_endianness



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.