7

The problem is if I console log this string: "Lorem " the console.log will output: Lorem and I can't tell if at the end of the string there is white space or not.

How can I force console to show output in quotation marks?

6
  • 1
    console.log('"Lorem "'); Commented Aug 20, 2019 at 17:35
  • You cannot really do that. You can just add delimiters and potentially overwrite the console.log to always do that. But it's probably too much of a hack to do for the whole application. Commented Aug 20, 2019 at 17:36
  • Use another console. Commented Aug 20, 2019 at 17:37
  • 2
    console.log(JSON.stringify("Lorem ")) Commented Aug 20, 2019 at 17:37
  • It actually prints with the space, it is just we don't see that. If you try highlighting the printed string you will see spaces getting selected. Commented Aug 20, 2019 at 17:42

4 Answers 4

12

Try

let s = "Lorem";
console.log( JSON.stringify(s) );

Sign up to request clarification or add additional context in comments.

1 Comment

I was afraid that this is the only best solution (which is the most readable) I think. Thanks.
6
let lorem = "Lorem ";
console.log('"'+lorem+'"');
// Prints: "Lorem "

More generally, the pattern console.log('"'+expression+'"') will print out the expression with quotes on either side so you can see where the actual beginning and end of the expression is.

If you want this to happen every time, consider creating a function like so:

function myConsole(...expr){
  console.log('"'+expr.join(' ')+'"');
}

And using that instead of console.log

2 Comments

If you remove ... and .join(' ') it will works too :)
It will for any unary call, but console.log is technically variadic, so someone expecting myConsole to work like console.log might reasonably attempt myConsole(anExpression, anotherExpression, aThirdExpression), which wouldn't work without the .... The .join(' ') is unnecessary except that it separates the arguments with a space.
3

Simply escape the slashes:

console.log("\"Lorem \"")

or wrap the double quotes in single quotes (or vice-verse)

console.log('"Lorem "');

Comments

1

To show single quote ( " ) with a string use this syntax

console.log("\"\Your string");

gives output as "Your string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.