In javascript, what is the difference between "\" and "\ n"?

I believe that both \ and \ n can be used as line breaks. What is the difference?

EDIT: Sorry, I realized that I was wrong . The tutorial talked about / as a line break for a programmer that didn't display, for example:

alert("Hi \
there");
+3
source share
3 answers

\nis the only correct return code for a new line (not counting the use of elements such as \u000aor similar).

Note that the code below does not actually insert line breaks (the actual \nescape code is still needed ):

var foo = 'qwertyuiopasdfghjkl\
qwertyuiopasdfghjkl\
qwertyuiopasdfghjkl';

"qwertyuiopasdfghjklqwertyuias". , . :

var foo = [
    'qwertyuiopasdfghjkl',
    'qwertyuiopasdfghjkl',
    'qwertyuiopasdfghjkl'
].join('\n');
+2

. . .

var foo = "This string \
has only \
one line.";

// Result:
// This string has only one line.

A "\n" .

var bar = "This string has\ntwo lines.";

// Result:
// This string has
// two lines.
+6

\n - ( ). , "Hello World!\nHow are you?" :

// Hello World!
// How are you?

:

// Hello World!How are you?

"\" ... \t, . \\, . \ .

+1
source

Source: https://habr.com/ru/post/1781861/


All Articles