Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Template Strings Don't Pretty Print Objects

Tags:

Can I use ES6 template strings to pretty print javascript objects? This is from a React Native project, with console.log() outputting to Chrome debugging tools.

What I Want

const description = 'App opened'; const properties = { key1: 'val1', blah: 123 }; console.log('Description: ', description, '. Properties: ', properties); 

outputs

Pretty printing

Template String Attempt

// Same description and properties const logString = `Description: ${description}. Properties: ${properties}`; console.log(logString); 

outputs

enter image description here

Question

How do I get the first output (with the pretty printing) using template strings?

like image 917
alexdriedger Avatar asked Sep 11 '17 00:09

alexdriedger


People also ask

Are template strings faster?

Template strings are now an order of magnitude faster than string concatenation. See version 14 of the given jsperf, it is the most accurate & unbiased one can technically get while retaining feature aspects.

What is the use of template string in JavaScript?

Template strings are a powerful feature of modern JavaScript released in ES6. It lets us insert/interpolate variables and expressions into strings without needing to concatenate like in older versions of JavaScript. It allows us to create strings that are complex and contain dynamic elements.

What are template strings used for?

Template literals are sometimes informally called template strings, because they are used most commonly for string interpolation (to create strings by doing substitution of placeholders).

What is template string in ES6?

Template literals are a new feature introduced in ECMAScript 2015/ ES6. It provides an easy way to create multiline strings and perform string interpolation. Template literals are the string literals and allow embedded expressions. Before ES6, template literals were called as template strings.


2 Answers

Your first example does not actually output a string to the console. Notice how properties is passed as a separate parameter argument (as it is surrounded by commas , and not string-concatenation operators +).

When you pass an object (or any JavaScript value) to console as a discrete argument it can display it how it wishes - including as an interactive formatted display, which it does in your first example.

In your second example, you're using templated-strings, but it's (generally) equivalent to this:

logString = "Description: " + description.toString() + ". Properties: " + properties.toString()"; 

And Object.prototype.toString() returns "[object Object]" by default. Note that this is a string value which is not particularly useful.

In order to get a JSON (literally JavaScript Object Notation) representation of an object used in a templated string use JSON.stringify:

logString = `Description: ${ description }. Properties: ${ JSON.stringify( properties ) }.` 

Or consider extending toString for your own types:

myPropertiesConstructor.prototype.toString = function() {     return JSON.stringify( this ); }; 
like image 147
Dai Avatar answered Oct 12 '22 23:10

Dai


Can I use ES6 template strings to pretty print javascript objects?

Sure, but you have to convert the object to a pretty printed version before you pass it to the template literal (I'm sure there are libraries out there that do that. The poor man's version is JSON.stringify(obj, null, 2)).

However, since console.log accepts an arbitrary number of arguments, you should just pass the object as second argument so that it doesn't get converted to a string:

const logString = `Description: ${description}. Properties:`; console.log(logString, properties); 
like image 20
Felix Kling Avatar answered Oct 13 '22 00:10

Felix Kling