javascript 如何向 console.log 添加变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16600925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:17:38  来源:igfitidea点击:

How can I add a variable to console.log?

javascriptconsole.log

提问by Bradley McInerney

I'm making a simple game in JavaScript but in the story I need it to say the players name. so what I have so far is:

我正在用 JavaScript 制作一个简单的游戏,但在故事中我需要它说出玩家的名字。所以到目前为止我所拥有的是:

var name = prompt("what is your name?");

console.log("story" name "story);

how do I do the second line? or there is another way I could do this. Is it possible to have 2 console.log();on 1 line in the console?

我如何做第二行?或者有另一种方法可以做到这一点。console.log();控制台中是否可以在 1 行中设置2 ?

回答by Joseph

Then use +to combine strings:

然后用于+组合字符串:

console.log("story " + name + " story");

回答by Coin_op

console.logtakes multiple arguments, so just use:

console.log需要多个参数,所以只需使用:

console.log("story", name, "story");

If nameis an objector an arraythen using multiple arguments is better than concatenation. If you concatenate an objector arrayinto a string you simply log the type rather than the content of the variable.

如果name是 anobject或 an,array则使用多个参数比串联更好。如果您将一个objector连接array到一个字符串中,您只需记录类型而不是变量的内容。

But if nameis just a primitive type then multiple arguments works the same as concatenation.

但是如果name只是一个原始类型,那么多个参数的工作方式与连接相同。

回答by Sanjib Debnath

You can use another console method:

您可以使用另一种控制台方法:

let name = prompt("what is your name?");
console.log(`story ${name} story`);

回答by mannutech

There are several ways of consoling out the variable within a string.

有几种方法可以消除字符串中的变量。

Method 1 :

方法一:

console.log("story", name, "story");

Benefit : if name is a JSON object, it will not be printed as "story" [object Object] "story"

好处:如果 name 是一个 JSON 对象,它不会被打印为 "story" [object Object] "story"

Method 2 :

方法二:

console.log("story " + name + " story");

Method 3: When using ES6 as mentioned above

方法 3:当如上所述使用 ES6 时

console.log(`story ${name} story`);

Benefit: No need of extra , or +

好处:不需要额外的 , 或 +

Method 4:

方法四:

console.log('story %s story',name);

Benefit: the string becomes more readable.

好处:字符串变得更具可读性。

回答by EinArzt

When using ES6 you can also do this:

使用 ES6 时,您还可以这样做:

var name = prompt("what is your name?");
console.log(`story ${name} story`);

Note: You need to use backticks `` instead of "" or '' to do it like this.

注意:您需要使用反引号 `` 而不是 "" 或 '' 来做到这一点。

回答by Lyn Headley

You can pass multiple args to log:

您可以将多个参数传递给日志:

console.log("story", name, "story");

回答by Bluedog111

It depends on what you want.

这取决于你想要什么。

console.log("story "+name+" story")will concatenatethe strings together and print that. For me, I use this because it is easier to see what is going on.

console.log("story "+name+" story")字符串连接在一起并打印出来。对我来说,我使用它是因为它更容易看到发生了什么。

Using console.log("story",name,"story")is similar to concatenation however, it seems to run something like this:

Usingconsole.log("story",name,"story")类似于 concatenation 但是,它似乎运行如下:

 var text = ["story", name, "story"];
 console.log(text.join(" "));

This is pushing all of the items in the array together, separated by a space: .join(" ")

这是将数组中的所有项目推到一起,用空格分隔: .join(" ")

回答by Sagar Kulkarni

Both console.log("story" + name + "story")and console.log("story", name, "story")works just fine as mentioned in earlier answers.

双方console.log("story" + name + "story")console.log("story", name, "story")在前面的答案中提到的工作就好了。

I will still suggest of having a habit of console.log("story", name, "story"), because, if trying to print the object contents, like json object, having "story" + objectVariable + "story"will convert it into string.

我还是建议养成一个习惯console.log("story", name, "story"),因为,如果尝试打印对象内容,如json对象,拥有"story" + objectVariable + "story"会将其转换为字符串。

This will have output like : "story" [object Object] "story".

这将有输出,如:"story" [object Object] "story"

Just a good practice.

只是一个很好的做法。

回答by Ben Anderson

You can also use printf style of formatting arguments. It is available in at least Chrome, Firefox/Firebug and node.js.

您还可以使用 printf 样式的格式化参数。它至少在 Chrome、Firefox/Firebug 和 node.js 中可用。

var name = prompt("what is your name?");

console.log("story %s story", name);

It also supports %d for formatting numbers

它还支持 %d 格式化数字