How to console log the name of a variable/function?

后端 未结 6 1462
梦谈多话
梦谈多话 2020-12-09 12:58

I want to do this: log(variableOrFunction)

And be able to produce this: variableOrFunction: actualValue.

I tried this:



        
6条回答
  •  没有蜡笔的小新
    2020-12-09 13:33

    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})

提交回复
热议问题