Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
In JavaScript, can be use a new line in console.log?
Yes, you can use newlines in console.log() using the escape character. This creates line breaks in console output.
Basic Newline Usage
console.log("First line\nSecond line\nThird line");
First line Second line Third line
Multiple Ways to Add Newlines
There are several approaches to include newlines in console output:
// Method 1: Using <br> escape character
console.log("Hello\nWorld");
// Method 2: Multiple console.log() calls
console.log("Hello");
console.log("World");
// Method 3: Template literals with actual line breaks
console.log(`Hello
World`);
// Method 4: Combining text with newlines
console.log("Name: John\nAge: 25\nCity: New York");
Hello World Hello World Hello World Name: John Age: 25 City: New York
Practical Example with Object
const studentDetailsObject = {
name: 'David',
subjectName: 'JavaScript',
countryName: 'US',
print: function(){
console.log('hello David');
}
};
console.log("Student Object:", "<br>", studentDetailsObject);
console.log("<br>--- Formatted Output ---");
console.log("Name: " + studentDetailsObject.name + "\nSubject: " + studentDetailsObject.subjectName + "\nCountry: " + studentDetailsObject.countryName);
Student Object:
{
name: 'David',
subjectName: 'JavaScript',
countryName: 'US',
print: [Function: print]
}
--- Formatted Output ---
Name: David
Subject: JavaScript
Country: US
Comparison of Methods
| Method | Usage | Best For |
|---|---|---|
escape |
Single console.log with breaks | Formatted strings, simple output |
| Multiple console.log() | Separate function calls | Different data types, debugging |
| Template literals | Backticks with actual breaks | Multi-line strings, complex formatting |
Conclusion
The escape character is the most common way to add newlines in console.log(). Use it for formatted output and better console readability.
Advertisements
