New line character \n

General question but I am working on Unit 4 Lesson 3 Level 6. Here are the given directions and code:
console.log(“How many days have you been alive?”);
// Add code here. There are 365 days in a year. Use \n to add spaces to your output.
console.log(“I have been alive " + (age * 365) + " days!”);

First, the comments say to use the \n to add spaces to your output. /n should create a new line and not spaces. Second, the \n has the odd behavior seen below for the output:
"I have been alive 5840 days!
"
As you see, the second set of quotation marks are placed on the next line. I don’t understand why that behavior is happening. the \n should only elicit a “carriage return”. If I add another line under this, this is what it looks like:
"How many months have you been alive?
"
"I have been alive 192 months!
"

What is happening here? I’ve tried using single quotes and concatenating + ‘\n’.

Thank you!

I have the same question.

Hi @l.leaming,

I think this is a bit confusing because console.log now prints and includes formatting to identify the data type - in other words, it automatically puts quotes around strings.

In this case, you see the closing quote enclose the final carriage return. Not sure if that answers your question.

1 Like

I agree that using the wording “to add spaces to your output” is confusing. It’s adding “blank lines” not spaces. Also as Frank says the " to end the string is on the next line in the output because the \n moved down to the next line before the string ended.

Another thing that is important to remember is that newline, carriage return, and linefeed are all different characters. Some combinations are valid line terminators and others are not. There is an etiquette.

I have a similar question regarding the string in the text property of a label. If I insert a ‘\n’ character in the middle of the string it does not do a new line, instead it just displays the two characters, “\n” in the middle of the line. Huh?

@lgroner -
hmm, I’m not sure I can reproduce this issue. If I type this line of code:
image

I receive this output:
image

Am I understanding your issue correctly?

setProperty(“label2”, “text”, “te\nxt”);
now seems to work displaying
te
xt
as the label. For some reason that did not work for a student.