Search code examples
javascriptauto-indent

Is the \n supposed to indent your text?


I'm a python programmer and in Python, the \n renders a new line like so:

print("Hello \n World")
Hello
World

But I'm trying to do that in Javascript with the following block of code:

if (userInput == wrongAnswer){
    let finalScore = initialScore + scoreForA;
    console.log(finalScore);
    if (finalScore == 5){
        console.log(rightScore);
    }
    else{
        console.log("Initial score:", initialScore, "\n", outputA, "\n", "Final score:", finalScore);
    }
}

Edit: By the way, I've defined these variables already.

But it gives me:

enter image description here

And I'm supposed to make it: enter image description here

Is the \n supposed to auto-indent Wrong answer and Final score after making a new line in JavaScript?


Solution

  • Note that you're providing multiple arguments to console.log, so the python analog would be print("Hello\n", "World") which would add a space on the second line as well. Multiple arguments are always separated by a space, if you don't like that, concatenate them or use a template literal:

    console.log(`Initial score: ${initialScore}\n${outputA}\nFinal score:${finalScore}`)