DEV Community

loading...

Logging like a PRO in javascript

ziishaned profile image Zeeshan Ahmd Updated on ・1 min read

Inside JavaScript there is a built-in function JSON.stringify which converts any variable into json string.

The syntax is:

JSON.stringify(value, replacer, space)
Enter fullscreen mode Exit fullscreen mode
  1. value - Any variable you want to convert into JSON string
  2. replacer - This method will be called on each item of the variable
  3. space - Number of spaces you want to add to each item of the variable

The second and thrird arguments are optional.

Lets have a look at the following example:

const users = [
  {
    name: 'John',
    isActive: false,
  },
  {
    name: 'Jane',
    isActive: true,
  },
];
console.log(JSON.stringify(users, null, 2));
Enter fullscreen mode Exit fullscreen mode

Upon running above code you will get the following output:

Output

Discussion (1)

pic
Editor guide
Collapse
stkdmitry profile image