简体   繁体   中英

escape sequences in strings in javascript

i want to write this sentence in 3 lines but on scrimba this does not work but on Repl.lt ( https://repl.it/repls/WickedBaggyRegression ) it works, why?

var myStr = 'FirstLine\n\\SecondLine\\\rThirdLine';
console.log(myStr);

It is probably due to how scrimba.com site does interpret and displays your log in its own custom console.
(it probably wrap the text console.log receives in a single line.)

Try by replacing console.log by alert and you will see escape sequences are "preserved" in the alert popup displayed in the simulated browser, which means, they are correct in a JavaScript way :

I also found a course that explain this: https://scrimba.com/p/p4Mrt9/c4vJdha

在此处输入图片说明

If you open up the browser console, you can see that the escape characters work perfectly fine. Please don't rely on the inbuilt console of tutorial sites, for the credibility of Javascript.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM