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);
AJ_
  • 3,787
  • 10
  • 47
  • 82

4 Answers4

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);
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
  • Yeah but i cant do stuff like this console.log("number:%d text:%s"par1,par2); – AJ_ Jan 13 '16 at 15:10
  • You can. See my updated answer. – ssc-hrep3 Jan 13 '16 at 15:19
  • 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. – AJ_ Jan 13 '16 at 15:24
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.

Sing
  • 3,942
  • 3
  • 29
  • 40
  • Sorry i meant, i wanted to created something that i can do this with. console.log("number:%d text:%s"par1,par2); – AJ_ Jan 13 '16 at 15:10
1

use arguments object. like this

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

p(1,2,3);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
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.

Mike Kor
  • 876
  • 5
  • 14
  • 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); – AJ_ Jan 13 '16 at 15:12