How to break JavaScript Code into several lines?

We can break JavaScript code into several lines by using the backslash () at the end of the line. This tells the JavaScript interpreter to continue reading the next line as part of the same statement. Additionally, we can also use parentheses, brackets, or curly braces to create multiple lines of code within a single statement. This helps to make the code more readable and easier to maintain. Let us first understand what code splitting is.

Code Splitting

Code splitting is a technique used in web development to load only the necessary code for a specific page or component, rather than loading an entire application at once. This can improve the performance and load times of a website or application. Code splitting can be done manually or using a tool such as webpack.

The main benefit of code splitting is that it reduces the initial load time and improves the overall performance of the application. It also allows for easy lazy loading of content.

Approach

  • JavaScript code can be broken into several lines using the line continuation character, which is a backslash () at the end of the line.

Here is an example −

let longString = "This is a long string that needs to be \
broken into several lines.";

Alternatively, you can use parentheses, brackets, or template literals to span your code over multiple lines without using the line continuation character −

let longString = (
   "This is a long string that needs to be " +
   "broken into several lines."
);
let longString = `This is a long string that needs to be
broken into several lines.`;