Home > Article > Web Front-end > How do I get the correct newline character in JavaScript?
The newline character is an important consideration when working with strings in JavaScript. Its purpose is to create a new line when printed. While 'n' is often assumed to be the universal newline character sequence in JavaScript, it's essential to understand that this may not hold true across all platforms.
The platform-specific newline character can vary. To determine the correct newline character for the current environment, use the following code snippets:
const os = require('os'); const newline = os.EOL;
const newline = document.createElement('textarea').value.substring(0, 1);
Here are some examples that demonstrate the use of newline characters in JavaScript:
const message = "Hello\nWorld!"; console.log(message); // Output: Hello // World!
const newline = os.EOL; const message = `Hello${newline}World!`; console.log(message); // Output: Hello // World!
The above is the detailed content of How do I get the correct newline character in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!