What is the usefulness of the console.dir() in Javascript?

Asked

Viewed 768 times

6

var body = document.getElementsByTagName("body");
console.dir(body)

Well, an interactive list of object properties will be returned, but what is the usefulness of this, what I’m trying to say is that we can call directly in the method document.getElementsByTagName("body") that the same result will be returned.

Besides the code economy there is some other utility for this function?

  • 1

    Good information on this method: https://developer.mozilla.org/en-US/docs/Web/API/console/dir and https://stackoverflow.com/questions/11954152/whats-the-difference-between-console-dir-and-console-log

2 answers

7


The console.log() and the console.dir(), today, do the same things. However, for some time, in firefox, the dir was tasked with printing the element tree, while the log was in charge of printing only one toString() of the element.

It is also important to pay attention to the note that it can in the documentation of MDN:

! Non-standard

This Feature is non-standard and is not on a standards track. Do not use it on Production sites facing the Web: it will not work for Every user. There may also be large incompatibilities between implementations and the behavior may change in the Future.

That is, the method console.dir() is not a standard method, which can cause problems in certain browsers that have not been implemented, and its use in production is not advisable.

In practice, today, with the changing shape that the console.log in its implementation, the console.dir became unnecessary.

2

If you are debugging the code for example from an Array and using the console.log it will give you the state of the object at that moment when you used the console.log.

If you use the console.dir you will see that regardless of where you have placed it in the code it will show the current state of the object.

Create an Array with some objects use the.log console to view the detailed array and then the.dir console of the same array. Then make some changes to the array and see what happens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.