DEV Community

Charlie @ 90-10.dev
Charlie @ 90-10.dev

Posted on • Originally published at 90-10.dev on

2 2

Handy console.log tip: display variables names

We're yet to see a JavaScript developer that doesn't use console.log to perform a "quick debug". And it goes like this:

let theAnswer = 42
console.log(theAnswer);
Enter fullscreen mode Exit fullscreen mode

And the console will oblidge, showing:

42
Enter fullscreen mode Exit fullscreen mode

Encapsulate in an object

Here is a handy tip: encapsulate the variable in an object and the variable name will be displayed:

console.log( { theAnswer } );
Enter fullscreen mode Exit fullscreen mode

The output will now be:

{theAnswer: 42}
Enter fullscreen mode Exit fullscreen mode

Happy debugging!

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay