126

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?

3
  • Are you just looking to concatenate strings together? What do you mean by "do the second line"? Commented May 17, 2013 at 3:46
  • ^ cogsmos, it doesn't matter now, my question has been answered Commented May 17, 2013 at 6:00
  • 1
    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: w3schools.com/js/js_operators.asp Commented May 17, 2013 at 6:09

11 Answers 11

136

Then use + to combine strings:

console.log("story " + name + " story");
Sign up to request clarification or add additional context in comments.

Comments

111

You can use another console method:

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

2 Comments

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...
For those noobs like me, ` is different than ' in javascript
95

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.

3 Comments

or also console.log('story %s story',name);
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.
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?
77

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.

2 Comments

@egemen what does freemarker have to do with it? It's an ECMAscript 2015 template literal
This is the best answer.
25

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.

Comments

6

You can pass multiple args to log:

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

Comments

4

%j works for only Node.js. %j converts a value to a JSON string and inserts it.

console.log('%j new messages for', 7, 'john')
// 7 new messages for john

More string substitutions here:

Docs:

Comments

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

Comments

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.

1 Comment

Then why are you suggesting "," one. As it will just print [object object]. Mostly, we want string version which will get frmo "+". Right?
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

Comments

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

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.