+ 3

Could someone please tell me why this is code didn't work? (no new line in the code playground)

var sayHello = 'Hello \n \r world! \'I am a JavaScript programmer.\' '; document.write(sayHello);

21st May 2017, 5:08 PM
Joe Lin
Joe Lin - avatar
9 ответов
+ 4
I think it would work if you replace the outer ' ' with " ".
21st May 2017, 5:47 PM
The Coding Sloth
The Coding Sloth - avatar
+ 4
I don't think so, try using console.log to see if it's going wrong at the string or at the document.write()
22nd May 2017, 5:27 AM
The Coding Sloth
The Coding Sloth - avatar
+ 3
It works fine for me, even with the ' '.
22nd May 2017, 5:08 AM
The Coding Sloth
The Coding Sloth - avatar
+ 3
console.log() is a function, if you use it the message inside the () will appear at the bottom of your screen (the console)
22nd May 2017, 6:27 AM
The Coding Sloth
The Coding Sloth - avatar
+ 2
Thank you guys for answer my question. the code was an example from sololearn. but even after I change the outter quote to " , it still doesn't work. any idea?
21st May 2017, 8:02 PM
Joe Lin
Joe Lin - avatar
+ 1
I think @The Coding Sloth answered already, you can't nest single quotes within single quotes and double quotes in double quotes unless you escape the internals. Best single quotes outside double quotes inside and vice versa.
21st May 2017, 6:13 PM
Benneth Yankey
Benneth Yankey - avatar
+ 1
I'm using Samsung S6, maybe that makes a difference?
22nd May 2017, 5:25 AM
Joe Lin
Joe Lin - avatar
+ 1
How do I find console.log in sololearn code playground?
22nd May 2017, 6:05 AM
Joe Lin
Joe Lin - avatar
+ 1
@the coding slotb, console.log() shows the new line correctly but document.write() doesn't start a new line. strange. thank you for showing me the other way to print
23rd May 2017, 5:07 AM
Joe Lin
Joe Lin - avatar