DEV Community

Zeeshan Ahmd
Zeeshan Ahmd

Posted on • Updated on

Logging like a PRO in javascript

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

Top comments (1)

Collapse
 
stkdmitry profile image
strelkov

)