Logging Aliases for Javascript

02 November 2021

Updated: 03 September 2023

I often find myself writing a function to JSON.stringify some data to log in either a pretty or flat structure

It’s just more of a convenience method, and it’s pretty much the same as doing console.log(JSON.stringify(data)) and looks like this:

1
const _ = (data: any, pretty: boolean = false) => {
2
return console.log(JSON.stringify(data, null, pretty ? 2 : 0))
3
}

And then, when I need to log something:

1
_(myData)

Or, if I want to pretty print the JSON

1
_(myData, true)