Open In App

Node.js os.endianness() Method

Last Updated : 13 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads