0

So as a C user, I really love using printf. Now that I'm doing js applications I'm forced to use console.log(). You can use console.log("User %s has %d points", userName, userPoints);, but i want to create my own function to do the job, because im making a few changes. So I want to create a function that takes multiple parameters, and print them out accordingly.

Example of what i want to do

function(parm1,parm2,parm3...){ //Stuff } console.log("number:%d text:%s"par1,par2); 
2
  • stackoverflow.com/questions/610406/… Commented Jan 13, 2016 at 15:12
  • @BLUEPIXY, omg i cant believe that i just didnt search JavaScript spring. Thanks !!! Commented Jan 13, 2016 at 15:19

4 Answers 4

2

Just use the default implementation of console.log(), because it does support multiple arguments, like it is described in the reference guide of the Mozilla Developer Network and in the reference guide of Google Chrome.

Your example does also work with console.log (taken from the official developer page of Google Chrome):

console.log("User %s has %d points", userName, userPoints); 
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah but i cant do stuff like this console.log("number:%d text:%s"par1,par2);
You can. See my updated answer.
Awesome. thanks BLUEPIXY also gave a good answer, i was trying to recreate my own console.log method, but this method works as well. Thanks, i wish i could have two answers.
1

use arguments object. like this

function p(){ for(var i = 0; i < arguments.length; ++i) console.log(arguments[i]); } p(1,2,3); 

4 Comments

Yup this works. Ill try to turn it into my own printf statement before downloading. github.com/alexei/sprintf.js
Why would you do that if it is initially supported by the browser (e.g. Google Chrome)?
However, i would like to create my own sprinf method not use another persons code.
@ssc-hrep3 I'm creating my own function.
1

console log can take multi arguments, so you can use like :

 var a = function(parm1,parm2,parm3,param4,param5){ console.log(parm1,parm2,parm3); } a(1,2,3)

you could run the snippets and see in your dev tool.

1 Comment

Sorry i meant, i wanted to created something that i can do this with. console.log("number:%d text:%s"par1,par2);
0

You can use similar output

console.log('param1 = ' + param1 = ' param2 = ' + param2); 

Also there is debug mode in most of browsers where you can use breakpoints.

1 Comment

Yeah i know i can do that. I don’t want to do it that way. I want to do it like printf, console.log("number:%d text:%s"par1,par2);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.