Escape Sequence Javascript

Tried many ways, and looked for many examples. For the returns, how should they be written? I couldn’t find examples to save my life, and when tried on my own the closest I came was 3 out of 4…

var myStr= "FirstLine\n\\SecondLine\\r\ThirdLine";

Does one of these help?:




Common escaped characters :
’ single quote
" double quote
\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed

I don’t understand what is wrong with my carriage return, or my double backslash. I tried all 3 resources, and then took to Google before posting.

Maybe you swapped the order of the r and the last \?

You should start billing me, Kev! I’m still getting one error, though…

myStr should have encoded text with the proper escape sequences and no spacing.

Close! Now you have swapped the order of \r and \\

1 Like

lol, when my brother asks for help and stuff I often say “At this point I should start billing you.”

1 Like

your code is…given below…

var myStr= “FirstLine\n\tSecondLine\n\ThirdLine”; // Change this line

So I am kinda confused here. The code listed in the curriculum is as follows:

var myStr ="FirstLine\n\t\\SecondLine\nThirdLine"; 

However this seems to only work in the FCC environment. I do it in let’s say VS Code or Codepen.io it will not accept the escape characters. They still appear in the code however, they do not actually execute. Why? Has this been deprecated? I have found no evidence of this nor any evidence to support why it only works in your environment. Please help, thank you.

PS. Here is a look at the code in Codepen.io

1 Like

@gavin It works, but browsers do not display \n like you are expecting. If you were to use console.log statement, then you would see it in the browser console.

So method of output is important to know then. Browsers do not display escape characters like they do in the console for some reason (which I must now research and know). I had no idea this was a thing. Thanks.

Most formatting is control with CSS. There is an html element <br> which creates a “new line”, but it is only used in special cases.

1 Like