2

console is not included in the list, but is pretty much available to use in any environment.

On a side note, why isn't console capitalized like every other built-in global object like Number or Array?

sdfsdf
  • 5,052
  • 9
  • 42
  • 75
  • `Number` and `Array` are constructor functions. `console` is a Web API, it's not a part of JS. – Teemu Aug 29 '18 at 05:53
  • 1
    https://console.spec.whatwg.org/ says "For historical reasons, `console` is lowercased." – Amadan Aug 29 '18 at 06:00
  • 3
    1) Because it is not mentioned in the spec: https://www.ecma-international.org/ecma-262/9.0/ . 2) Maybe because it is not a constructor but a "normal" function? – Felix Kling Aug 29 '18 at 06:00
  • 1
    @FelixKling It's not even a function, it's just a normal object (acting as namespace) – Amadan Aug 29 '18 at 06:02
  • @Amadan: Whoops, somehow I thought about `console.log`. It's late, I'm tired :P – Felix Kling Aug 29 '18 at 06:04
  • @Amadan This note is for the namespace definition using "console" and not "Console", not the exposed api on the `window` object. I've elaborated more in my answer – Bamieh Aug 29 '18 at 12:40
  • 1
    @Bamieh: It was an answer to another comment that was changed, saying that it's lowercase `console` because it's not a constructor function. But since you've drawn my attention to your elaboration, "All methods provided by the whatwg spec implementing the window interface are not capitalized" is not an explanation, since, as I've already had to note above, `console` is not a method. – Amadan Aug 29 '18 at 12:43
  • 1
    @Bamieh: Also, `window.console` _is_ the namespace they're talking about. It is an object whose sole purpose is to group various console-related methods. They are not two different things, as you imply in 'This note is for the namespace definition using "console" and not "Console", not the exposed api on the `window` object'. – Amadan Aug 29 '18 at 12:46
  • @Amadan Agreed, they are not capitalized because they are not constructor functions. – Bamieh Aug 29 '18 at 12:48

1 Answers1

7

ECMA-262 doesn't define console because ECMA-262 has no concept of I/O. Simply each browser implements/injects its own console implementation.

The console object was first introduced by browser debugging tools, Firebug was the first to try to formulate a consistent standard for the console api.

The WHATWG (Web Hypertext Application Technology Working Group) has an early work in progress console spec to define the semantics of the console APIs, in an attempt to create convergence across environments.

console is not included in the list, but is pretty much available to use in any environment.

The console object is somehow consistent between browsers due to the following:

  • Browsers and Node.js all individually follow the WHATWG console spec.

  • Chrome extends WebKit which is also used by Safari.

  • Node.js is built on top of V8, which defines the console API used by node.

Currently many of the console methods are cross-browser compatible, however this was not always the case. Check the compatibility table to see the differences.

Arguably, each console implementation differs based on the environment it is served in, (chips, servers, browsers, etc.). Hence it does not make complete sense to have it standardized in ECMA.

Just like other browser specific APIs, the console is injected to javascript to give developers access to the browser API, such as Node and Document.

Here is a list of the complete web API injected into javascript to be accessible in JS code on the browser.

why isn't console capitalized like every other built-in global object?

All methods provided by the whatwg spec implementing the window interface are not capitalized (Check the window interface), Since non of those methods are constructor functions.

It is also mentioned in the spec that the console is lowercased due to historical reasons. However this note is talking about the namespace definition using "console" instead of "Console" used in the spec and NOT the exposed API.


FUN FACT: I remember the time when the console used to throw an error if the debugger was not opened on IE.

Bamieh
  • 10,358
  • 4
  • 31
  • 52
  • 1
    Great answer! If I may offer a few corrections: 1) ECMA-262 doesn't define `console` because ECMA-262 has no concept of I/O. 2) browsers and node all individually follow the whatwg console spec, not by browsers happening to be built on other browsers as you suggest. 3) whatwg console spec is already ratified, so no need to use future tense "will be part of the whatwg spec" – snek Aug 29 '18 at 12:49
  • @snek thank you! 1) You are correct, please feel free to update the answer. 2) I referred to this line in the spec "This specification is an early work in progress" But indeed it is part of the spec (updated answer). – Bamieh Aug 29 '18 at 12:52
  • 1
    @snek Surely the `console` implementations of the different environments converged before it became a whatwg standard - either because implementations inherited from each other or because they followed [the standard set by the Firebug extension](https://web.archive.org/web/20171214102729/https://getfirebug.com/wiki/index.php/Console_API) (unfortunately no longer online). Here's a bit of history overview: http://2ality.com/2013/10/console-api.html – Bergi Aug 29 '18 at 13:08
  • 1
    @Bergi definitely. the clarification i was trying to make is that chrome and safari and node.js etc all have the same console interface *today* because they all willfully follow the same spec. it isn't just because of historical copycat reasons. – snek Aug 29 '18 at 13:23