JavaScript Node.js Basics Building a Command Line Application Making a GET Request with https

James Estrada
James Estrada
17,821 Points

console.dir() vs console.log()

I'm using VS Code and I don't see any difference in the console whenever I use console.dir() or console.log(). According to this stackoverflow solution: https://stackoverflow.com/questions/11954152/whats-the-difference-between-console-dir-and-console-log I should see a tree representation of the output with console.dir(), but I see the same thing with both methods even with the example of the arrays the solution suggested. Is console.dir() a method I should use instead of console.log() whenever I use Node.js? Because I have not seen this method used in any other previous Treehouse courses from the Full Stack JavaScript track until this Node.js Basics course.

2 Answers

Steven Parker
Steven Parker
177,931 Points

In the past, the behavior was quite different, particularly when logging object contents, but I've noticed that newer JavaScript engines have expanded the functionality of "console.log" so there isn't much different from "console.dir" anymore.

Routine Poutine
Routine Poutine
26,034 Points

Should console.dir() be deprecated if console.log() can replace it?

Steven Parker
Steven Parker
177,931 Points

I haven't checked on the recent official language specs. It could be that the extra features are not required by the spec, and might not be implemented the same way in all browsers. In that case, official deprecation is probably not appropriate at this time.