• Node.js Video Tutorials

Node.js - os.EOL Property



The Node.js os.EOL property stands for operating system-specific end-of-line marker. This is used to get the end-of-line marker or character as specified by the operating system.

If we try to run the os.EOL on the windows operating system will return "\r\n" to the output. Else if we run it on POSIX operating system, it will return "\n" to the output.

Syntax

Following is the syntax of the Node.js os.EOL property −

os.EOL

Parameters

The os.EOL does not accept any parameters.

Return value

It returns the EOL (end-of-line marker) which is specified by the operating system.

Now let's dive into the examples of the os.EOL of Node.js in different scenarios.

Example

In the following example, we are trying to print the os.EOL property by using JSON.stringify. If we try to print without stringifying it, it will print as the end of line.

const os = require('os');
const { EOL } = os;
console.log(JSON.stringify((os.EOL)));

Output

"\n"

Note − To get the accurate result, better execute the above code in local.

If we compile and run the above program, os.EOL prints as shown in the below figure.

"\r\n"

Example

In the example below, we are printing the os.EOL along with a string. So, it will behave similarly to a 'new line'.

const os = require('os');
const { EOL } = os;
console.log('Welcome to Tutorialspoint' + os.EOL + 'Hope you have a great learning here!');	

Output

As we can see in the output figure below, the string before the os.EOL printed and then os.EOL printed end-of-line, so the string after the os.EOL will print in a new line below.

Welcome to Tutorialspoint
Hope you have a great learning here!

Example

In the following example, we are printing the os.EOL character(s) along with a set of strings.

const os = require('os');
const { EOL } = os;
console.log('The calender months are' + os.EOL + "JANUARY" +  os.EOL + "FEBRAURY" + os.EOL + "MARCH" + os.EOL + "APRIL" + os.EOL + "MAY" + os.EOL + "JUNE" + os.EOL + "JULY" + os.EOL + "AUGUST" + os.EOL + "SEPTEMBER" + os.EOL + "OCTOBER" + os.EOL + "NOVEMBER" + os.EOL + "DECEMBER");

Output

If we compile and run the above program, a set of strings will be printed along with os.EOL. The os.EOL prints the end-of-line so the next string after every os.EOL will be printed in the next line.

The calender months are
JANUARY
FEBRAURY
MARCHAPRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER
nodejs_os_module.htm
Advertisements