How to break string into multiple lines in javascript

By
javascript new line
In Javascript there are lots of thing which is not technical but all us have to know about this. Here we discuss very easy but very important point in javascript.
One day I am working with JavaScript and in this i declare a string but string is too large and  i don't want to write it in single line then in my mind how i break string into multiple line?
If i don't break into multiple line then horizontal scroll takes place in our IDE.So here an example of JavaScript
new line or new line in JavaScript or you may say that JavaScript break line.
In JavaScript basically there are two way to break string into multiple lines.


1.New Line in JavaScript with BackSlashing
2.String Concatenation

Break Line in JavaScript/Jquery

Backslashing : In backslashing you have to add "\" at the end of each line .It tells the JavaScript engine that string will not terminated at this line it continue to the next line.

var strMultiline="I already faced the same scenario. \
                            If i don't break into multiple line then horizontal \
                            scroll takes place in our IDE";

String Concatenation : String concatenation is may be easier on your eyes, but slower and more memory intensive .But so many developers used this but this is ugly as well as slow approach.

var strMultiline = "I already faced the same scenario."
                             + "If i don't break into multiple line then horizontal"
                             + "scroll takes place in our IDE";

Conclusion

If speed and performance is a goal than you should always go with your JavaScript strings with backslashes.Many novice developer don't know about Backslash approach but by performance wise its better than string concatenation.

1 comment: