6

I find myself typing this repeatedly for debugging in my code. Is there a way to create a function for this?

var abc = 1
console.log("abc = "+abc);

var xyz = 2;
console.log("xyz = "+xyz);

I would like to create a function like this:

logVar = function(input){
  console.log(input.name()+" = "+input);
}

Is there a way to do this?

logVar(abc) should return "abc = 1"

logVar(xyz) should return "xyz = 2"

Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
Wes Tomer
  • 319
  • 1
  • 13
  • Is there any functional reason to do this other than just manually typing the variable name as a string? – Sterling Archer Jun 24 '19 at 13:36
  • 4
    Not possible. JS doesn't hold the "name of the variable" value anywhere with the variable. What you are passing is just the value of it. What you probably need is a debugger, rather than having less code to type in order to debug stuff. – VLAZ Jun 24 '19 at 13:37
  • 2
    https://stackoverflow.com/a/48087922/4925008 – Dinosan0908 Jun 24 '19 at 13:39
  • Similar question asked and answered here https://stackoverflow.com/a/52598270/11465123 – Benoit F Jun 24 '19 at 13:41
  • how about this workaround: `console.log({ abc, xyz });` – Thomas Jun 24 '19 at 13:49

4 Answers4

9

You have to enclose you var in a object to get its name as a key:

var myVar = 'John Doe';

console.log({myVar}); // result {"myVar": "John Doe"}
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
4

You can create an object from your variable which you pass into logVar.Then, in your function, you can use Object.entires to get the name of the variable and the value of the variable.

See example below:

var logVar = function (input) {
    var [[name, val]] = Object.entries(input);
    console.log(name, "=", val);
}

var abc = 1;
var xyz = 2;

logVar({abc}); // abc = 1
logVar({xyz}); // xyz = 2
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Though this is not a proper solution, you can loop over the window object, but you can get other identifiers as well which hold the same value as the passed in argument. This only works in global scope & not while in function scope.

var ident = "wowowowow";
console.log(getIdentifierName(ident));

function getIdentifierName(identifier) {
  for (var prop in window) {
    if (window[prop] === identifier) {
      console.log(identifier);
    }
  }
}
Danyal Imran
  • 2,515
  • 13
  • 21
-1

This should work:

var abc = 1;
logVar = function(input) {
  console.log(input);
}
logVar({
  abc
});

The output should be something like : { abc: 1 }

shrys
  • 5,860
  • 2
  • 21
  • 36
S. Stumm
  • 290
  • 1
  • 9
  • 2
    what is the purpose of `logVar`? It's just doing `console.log` but limits it to a single argument. – VLAZ Jun 24 '19 at 13:48
  • its what he asked for. I do not know why he want's it like that. – S. Stumm Jun 24 '19 at 13:49
  • Well, he also asked for the output to have a `=` but yours doesn't. If you're deviating in the implementation in one place, it seems strange to keep up to it in another. – VLAZ Jun 24 '19 at 13:50
  • 1
    this solution has the same effects as `console.log({abc})` – leoap Jun 24 '19 at 14:34