最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show string output in console.log with quotation marks? - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Aug 20, 2019 at 17:39 Kamil Kiełczewski 92.4k34 gold badges394 silver badges370 bronze badges asked Aug 20, 2019 at 17:34 TwistedOwlTwistedOwl 1,3242 gold badges18 silver badges33 bronze badges 6
  • 1 console.log('"Lorem "'); – silencedogood 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. – VLAZ Commented Aug 20, 2019 at 17:36
  • Use another console. – Jonas Wilms Commented Aug 20, 2019 at 17:37
  • 2 console.log(JSON.stringify("Lorem ")) – Emeeus 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. – Saad Sahibjan Commented Aug 20, 2019 at 17:42
 |  Show 1 more comment

4 Answers 4

Reset to default 11

Try

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

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

Simply escape the slashes:

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

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

console.log('"Lorem "');

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

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

gives output as "Your string

发布评论

评论列表(0)

  1. 暂无评论