0

I am trying to write a convenience wrapper for console.log, and I would like to print any variables passed in along with their contents.

Can I turn a variable name into a string in js?

Jehan
  • 2,701
  • 2
  • 23
  • 28

2 Answers2

1

Assuming you want something like this:

function Log(data)
{
    console.log(input variable name, data);
}

Then I don't think it is possible:

For convenience.. you could do something like

console.log({ "your variable name": your variable});

Which turns the input to an object that does contain the variable name you want to log. A little more typing, but perhaps makes the console output more readable.

user1600124
  • 440
  • 2
  • 11
0

There is a possibility. And here is how

var passed_variable = '65'; // The actual variable
var varname = 'passed_variable'; // The name of the variable in another variable

Now, pass the varname around but not the actual variable. When you need to the value of the variable you can simply do :

console.log(varname, ' : ', window[varname]); // Outputs, passed_variable : 65

I hope you find a way not to use this. :)

Starx
  • 77,474
  • 47
  • 185
  • 261
  • unless `passed_variable` resides in the global space, it won't be visible as `window[varname]`. And I don't want to have to put my variables in the global scope in order to log them. – John Dvorak Sep 29 '13 at 05:33
  • and, of course, prefixing something with `var` is something you do when you _don't_ want it to end up in the global scope - in which case the log explicitly won't work (unless the code itself is in global scope, in which case the `var` keyword is merely pointless) – John Dvorak Sep 29 '13 at 05:49
  • @JanDvorak, Yeah the implication is it being in a global scope. – Starx Sep 29 '13 at 05:59
  • which `var` does the exact opposite of. Even if this is fixed, though, it's still a bad way to do your average-day logging. – John Dvorak Sep 29 '13 at 06:03
  • @JanDvorak No `var` used inside a function or scope limiter does that. – Starx Sep 29 '13 at 06:04
  • if you want something to be explicitly global, why not use `window.passed_variable = ...` rather than saying `var` and praying the snippet does end up in the global scope? What do you mean by "scope limiter"? – John Dvorak Sep 29 '13 at 06:06
  • @JanDvorak, Fine then use `window.passed_variable`, I prefer initializing variables with `var`. You do not need to make big fuss about this. By scope limiter, I meant anything that limits one's scope to outside. Some examples being functions, others anonymous callback functions, objects etc... – Starx Sep 29 '13 at 06:13
  • rhetorical question: why do you prefer `var` over `window.`? And the answer is: because `window.` makes stuff global while `var` restricts the variable to the local scope. Anyways, exporting stuff to the global scope just to be able to be lazy about logging is a bad idea. – John Dvorak Sep 29 '13 at 06:16
  • @JanDvorak, Where was the topic of Logging Jan, Why are drifting off shore here? – Starx Sep 29 '13 at 06:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38257/discussion-between-jan-dvorak-and-starx) – John Dvorak Sep 29 '13 at 06:29