Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 817087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:56:17+00:00 2026-05-15T01:56:17+00:00

UPDATE: Here’s my solution (inspired by accepted answer): function log(msg, values) { if(config.log ==

  • 0

UPDATE: Here’s my solution (inspired by accepted answer):

function log(msg, values) {
         if(config.log == true){
         msg = [msg];
         var args = msg.concat(values);
         console.log.apply( this, args );
         }
     }

UPDATE2: Even better solution:

 function log(msg) {
     if(config.log == true){
     msg = [msg];
     var values = Array.prototype.slice.call(arguments, 1);
     var args = msg.concat(values);
     console.log.apply( console, args );
     }
 }

You can call this version like so:

log("Hi my name is %s, and I like %s", "Dave", "Javascript");

Here’s the original question:

console.log takes a string and replaces tokens with values, for example:

console.log("My name is %s, and I like %", 'Dave', 'Javascript')

would print:

My name is Dave, and I like Javascript

I’d like to wrap this inside a method like so:

function log(msg, values) {
  if(config.log == true){
    console.log(msg, values);
   }
 }

The ‘values’ arg might be a single value or several optional args. How can I accomplish this?

If I call it like so:

 log("My name is %s, and I like %s", "Dave", "Javascript");

I get this (it doesn’t recognize “Javascript” as a 3rd argument):

 My name is Dave, and I like %s

If I call this:

 log("My name is %s, and I like %s", ["Dave", "Javascript"]);

then it treats the second arg as an array (it doesn’t expand to multiple args). What trick am I missing to get it to expand the optional args?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:56:17+00:00Added an answer on May 15, 2026 at 1:56 am

    Though this isn’t jQuery related, I can try answer it anyway 🙂

    Following might work (untested):

    function log(msg, values) {
      if(typeof config.log != 'undefined' ) {
        if( typeof values != 'Array' ) {
          values = [values];
        }
        values.unshift( msg );
        console.log.apply( this, values );
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my chunk of code: var update_shipping_methods = function(methods) { $(methods).each( function(i) {
Update: This question was an epic failure, but here's the working solution. It's based
Here's my code: $(document).ready(function(){ var Bold=Yes! There is bold in the text above!!; var
UPDATE Here Ajax receives some responseimage from the server success: function(responseimage) { ..... }
UPDATE: Here is the entire function I'm using when a person click to advance
* UPDATE * Here is what I found. Whenever I had that function in
Update : Here is the project. I have the following directory structure: RMI |
UPDATE: Based on suggestions here, I've modified my code. To get a handle on
Update I'm trying to extract the Tweet Per Hour information from here . However,
Here is my upsert code: UPDATE LastTicket SET LastTicketNumber=LastTicketNumber+1 OUTPUT INSERTED.LastTicketNumber WHERE CategoryId='1'; IF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.