How to console log the name of a variable/function?
Would this work for you?
const log = function() {
const key = Object.keys(this)[0];
const value = this[key];
console.log(`${key}:${value}`);
}
let someValue = 2;
log.call({someVlaue}); //someValue:2
Works with function too, even itself.
log.call({log});
// It would return the following
log:function() {
const key = Object.keys(this)[0];
const value = this[key];
console.log(`${key}:${value}`);
}
If “value” is of type Object. You can try console.dir(value) instead of the long console.log, and you should be able to see the Object tree key: value pairs in a better formate in the console
You can log them with a pair of braces around them ({}
), which will create an object with the name of the variable as the key:
function someFunction() {};
const someOtherFunction = () => {};
const someValue = 9;
console.log({someFunction});
console.log({someOtherFunction});
console.log({someValue});
const renamed = someFunction;
console.log({renamed})