Logging Values With Their Names

Not every bug needs the whole nested result that console.dir() showed. Say the positive check in Joan’s parseAmount() goes missing in an edit, and the total drifts from the 125 she expects. Here’s how the Python you’d use maps onto console:

Python JavaScript
print(f"{total=} {donor=}") console.log({ total, donor })
print(..., file=sys.stderr) console.error(...)
one print() per record console.table(rows)
traceback.print_stack() console.trace()

Label Each Value With Braces

Wrap the names in braces. { total } is object shorthand for { total: total }, so the variable names become the labels:

Language: JavaScript
const total = 125;
const donor = "D-101";
console.log({ total });
console.log({ total, donor });

Each call logs an object:

Language: Program Output
{ total: 125 }
{ total: 125, donor: 'D-101' }

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.