Return space in Javascript?

Hello!

I am working on the Magic ball project and wanted to have a return space between two of my console logs. Is there a command similar to
in HTML in javascript that adds space when it is printed?

let userName = ''; userName ? console.log(`Hello, ${userName}!`): console.log('Hello, stranger!'); let userQuestion = 'What do you want to ask?'; userName ? console.log(`${userName} asked the MagicBall "${userQuestion}"`): console.log(`The stranger asked the MagicBall "${userQuestion}"`)
console.log(`A new line starts in 3, 2, 1 \n You\'re probably looking for the combination of a backslash and the letter n. \n Bth welcome to the forums! `)
2 Likes

@mirja_t, since you are using backticks, there is no need to escape the apostrophe in You\'re. Just saying.

Line breaks are white space, along with space characters.

1 Like

Thank you for your response!

I meant more like in between separate console.log’s vs. in one string in one console.log. I guess I can just add a new console.log and leave it empty with space? Or just add a /n to first one to get around with it? Wouldn’t that clutter my code though if there’s a lot of content?

Depends on what you consider clutter, I suppose. You can get around needing to use \n, by typing your string literal on multiple lines:

console.log(` Me: Hello, World! How are you today? World: Well, I'm fine. Thank you for asking. Me: You're quite welcome. `);

So there is no way to add space in between two separate console.log versus in one string in one console.log ?
In my example, both codes can’t be merged (or as of now, I can’t merge them) I need them separate so I was wondering if there’s any command that is its own thing (meaning it doesn’t need to be implemented inside of a string) that will print as space

The console is hardly a place to worry about pretty printing. The advice given above is more than adequate, especially if we include interpolation with ${expression}. There are no special ‘commands’.

Logging is generally meant as a debugging tool to help us trace issues and errors. Perhaps you could fill us in more about what it is you are trying to do?

1 Like

Why not?

let userName = ''; let userQuestion = 'What do you want to ask?'; console.log(`Hello, ${userName ? userName : 'stranger'}! ${userName ? userName : 'The stranger'} asked the MagicBall, "${userQuestion}" `)

As @mtf said, you’ve been shown many ways to add blank lines in the console. It does seem that you have something in mind that you think should be doable, but doesn’t appear possible. If you know that you want to have a blank line appear after every console.log() you could write your own function to accomplish that. Something like:

// If you haven't studied functions yet, this // may be confusing. It's a concise body arrow // function. It simply uses console.log to print // whatever argument is supplied, and adds // a new line to the end. const print = arg => console.log(arg + '\n'); let userName = ''; print(userName ? `Hello, ${userName}!` : 'Hello, stranger!'); let userQuestion = 'What do you want to ask?'; print(userName ? `${userName} asked the MagicBall, "${userQuestion}"` : `The stranger asked the MagicBall, "${userQuestion}"`) print(`You\ncan\nstill\nmove\ndown\na\nline\nor\nadd\nblank\n\nlines\nusing\n\\n`);
1 Like