Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Node.js os.endianness() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:




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

Output:

LE

Example 2:




// 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");
}

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
Last Updated : 13 Oct, 2021
Like Article
Save Article
Similar Reads
Related Tutorials