Reputation: 734
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 console log it appears as:
counter 1
counter 2
and so on ....
Upvotes: 1
Views: 2020
Reputation: 21
You could also console log them out inside of an object, so that you're able to have access to its name and value.
var counter = 5;
input:
console.log({counter});
output:
{counter: 5}
Upvotes: 2
Reputation: 715
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
}
Upvotes: 0
Reputation: 378
You can use the label string followed by variable name and a "+" operator in between, as follows:
console.log("Counter : " + counter);
Upvotes: 0
Reputation: 26557
Not directly, but you can just name them when you output them.
console.log
(and .error
, .info
, and .warn
) let you pass any number of values at the same time, so it's super easy to just do something like this:
console.log('counter', counter);
which would output like:
counter 1
let counter = 0;
for (let i = 0; i < 5; i++) {
counter++;
console.log('counter', counter);
}
Upvotes: 3