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 4088254
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:57:31+00:00 2026-05-20T18:57:31+00:00

I want to create a quick function that will console.log a variable name and

  • 0

I want to create a quick function that will console.log a variable name and the value. I’d like the result of the function to show in the console: foo: bar.

My basic idea for the function looks like this:

function varlog(var_name)
{
    console.log(var_name + ": " + eval(var_name));
}

And I’d call is thusly:

function someRandomFunction()
{
    var foo = "bar";
    // ... some stuff happens
    varlog("foo");
}

This works if foo is global, but doesn’t work in the example provided. Another option that also only works globally is using window[var_name] instead of the scary eval.

I don’t think what I’m asking is possible, but I figured I’d throw it out there.

I’m spending a lot of time attempting to be lazy. My current method is just console.log('foo: ' + bar); which works just fine. But now I just want to know if this is possible.

Some other questions I referenced in searching for this / creating what I have now:

  • Variable name as a string in Javascript
  • How to convert variable name to string in JavaScript?
  • Javascript, refer to a variable using a string containing its name?
  • How to find JavaScript variable by its name

—

Edit: I’d love to just call varlog(foo), if the name “foo” can be derived from the variable.

  • 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-20T18:57:32+00:00Added an answer on May 20, 2026 at 6:57 pm

    The reason it doesn’t work is because the variable foo is not accessable to the function varlog! foo is declared in someRandomFunction, and is never passed into varlog, so varlog has no idea what the variable foo is! You can solve this problem by passing the variable foo into the function(or using some sort of closure to make foo in the scope of varlog) along with its string representation, but otherwise, I think you are out of luck.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone know of a script or already built function that will help me create
I am trying to create an initialisation function that will call multiple functions in
Background In Java, I want to make a simple, quick console app that provides
I want to create a function that replaces random words in a string. Here
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I want to create a new field (or two) in my table that is
I would like to create a plugin that uses the contact form 7 hook,
I want to create a field in my database which will be easy to
I want to create a form with a number of textareas. The user will,

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.