Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

When to use console.log or console.parse ( obj )?


Asked by Kaylie Dixon on Dec 01, 2021 FAQ



Don't use console.log (obj), use console.log (JSON.parse (JSON.stringify (obj))). This way you are sure you are seeing the value of obj at the moment you log it. Otherwise, many browsers provide a live view that constantly updates as values change. This may not be what you want.
One may also ask,
Console.log () Method – Log an Object in console Console.log () method is one of the most popular and widely used method to do this job. Using console.log () method, you can print any object to the console. This method will also work on any present browser.
Additionally, Don't use console.log (obj), use console.log (JSON.parse (JSON.stringify (obj))). This way you are sure you are seeing the value of obj at the moment you log it. Otherwise, many browsers provide a live view that constantly updates as values change. This may not be what you want. See implementation notes.
Besides,
Please be warned that if you log objects in the latest versions of Chrome and Firefox what you get logged on the console is a reference to the object, which is not necessarily the 'value' of the object at the moment in time you call console.log (), but it is the value of the object at the moment you open the console.
Also,
The solution is to copy the content of the object when printing it to the console. There are a number of ways to do that in plain JavaScript. We are going to see one using the stringify and parse methods of the JSON object.