Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a variable to console.log?

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?

like image 417
Bradley McInerney Avatar asked May 17 '13 03:05

Bradley McInerney


People also ask

How do you write a variable in console log?

log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);

How do I print two variables in console log?

When we want to print a message containing multiple variables along with a message, we can format console. log() accordingly by: Using specifiers like %s, %i, %f etc.

How do you add a line in console log?

To create a multi line strings when printing to the JavaScript console, you need to add the new line character represented by the \n symbol. Alternatively, you can also add a new line break using Enter when you use the template literals syntax.

How do I use console log code?

console. log() is used as a debugging tool to help you understand what your code is doing. By displaying a message containing either descriptive text that tells you what is happening or the value of particular variables, you can follow along as your code executes. The user of your app will not see the console.


1 Answers

Then use + to combine strings:

console.log("story " + name + " story"); 
like image 78
Joseph Avatar answered Sep 21 '22 04:09

Joseph