Console.log is there a way to name sent variables?

后端 未结 3 1843
死守一世寂寞
死守一世寂寞 2021-01-24 20:28

When sending items to console.log is there a way to name them? Similar to \"watch\" in visual studio

for example we have a var counter=1; so that in the co

3条回答
  •  逝去的感伤
    2021-01-24 20:43

    There is one workaround

    function p(variableInObject) {
        let name = Object.keys(variableInObject)[0]
        let value = variableInObject[name] 
        console.log(name, value)
    }
    let g = 5
    p({g}) // g 5
    // it even works with loops
    for (let i = 0; i < 3; i++) {
      p({i})  // i 0, then i 1, then i 2
    }
    

提交回复
热议问题