112

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:

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?

Ian
  • 50,146
  • 13
  • 101
  • 111
Bradley McInerney
  • 1,341
  • 4
  • 15
  • 14
  • Are you just looking to concatenate strings together? What do you mean by "do the second line"? – cogsmos May 17 '13 at 03:46
  • ^ cogsmos, it doesn't matter now, my question has been answered – Bradley McInerney May 17 '13 at 06:00
  • Glad you figured it out. In JavaScript the + can be used for arithmetic and also for joining two strings together (also called concatenation). Take a look here for some more details: http://www.w3schools.com/js/js_operators.asp – cogsmos May 17 '13 at 06:09

11 Answers11

126

Then use + to combine strings:

console.log("story " + name + " story");
Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
Joseph
  • 117,725
  • 30
  • 181
  • 234
99

You can use another console method:

let name = prompt("what is your name?");
console.log(`story ${name} story`);
Sanjib Debnath
  • 3,556
  • 2
  • 22
  • 16
  • 6
    Template strings are awesome. But provided someone uses ES6. Since in the example "var" is used (could be ES6 ofcourse) but don't think we can assume that here... – therebelcoder May 15 '17 at 12:27
92

console.log takes multiple arguments, so just use:

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

If name is an object or an array then using multiple arguments is better than concatenation. If you concatenate an object or array into a string you simply log the type rather than the content of the variable.

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

Coin_op
  • 10,568
  • 4
  • 35
  • 46
  • 22
    or also `console.log('story %s story',name);` – Muhammad Umer Jul 14 '14 at 03:33
  • 1
    Minor thing, but if you're in a less conventional setting like Parse's Cloud Code sending an array or using %s doesn't work.. so you're forced to concatenate. – Derek Lucas Jul 18 '14 at 19:48
  • That's great @MuhammadUmer, I thought that was only a Python thing. In Python `%s` is the best method for concatenating strings, since you don't need to change the variable type first. I wonder if it's the same in JS? – JasTonAChair Sep 22 '15 at 09:40
66

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"

Method 2 :

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

Method 3: When using ES6 as mentioned above

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.

mannutech
  • 912
  • 7
  • 7
23

When using ES6 you can also do this:

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.

Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
EinArzt
  • 347
  • 3
  • 11
6

You can pass multiple args to log:

console.log("story", name, "story");
Lyn Headley
  • 11,368
  • 3
  • 33
  • 35
2

It depends on what you want.

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

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

 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(" ")

hmit
  • 152
  • 10
1

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

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.

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

Just a good practice.

Grey
  • 877
  • 9
  • 29
Sagar Kulkarni
  • 2,072
  • 2
  • 16
  • 25
  • Then why are you suggesting "," one. As it will just print [object object]. Mostly, we want string version which will get frmo "+". Right? – Satish Patro Mar 30 '19 at 04:13
0

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

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

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

It also supports %d for formatting numbers

jmartori
  • 384
  • 1
  • 5
  • 14
Ben Anderson
  • 7,003
  • 4
  • 40
  • 40
-4

You can use the backslash to include both the story and the players name in one line.

var name=prompt("what is your name?"); console.log("story"\name\"story");

kimxons
  • 1
  • 5