1

The code is pretty simple, basically I assign the id given to the DOM element and show the output and the 'typeof' of the element as well.

 <li id="noob">N00B</li>
 <li id="coder">C0D3R</li>

var someVar = noob;
console.log(someVar); // output <li id="noob">
console.log(typeof someVar); // output object

From the tuts I have been using (mainly stackoverflow, w3schools, codeacademy and MDN) do I see mentioned that an id from a element is directly accessible by Javascript and can be assigned as a variable, at least without some sort type of method (ie. getElementById, getElementsByTagName, getAttribute).

When checking typeof it makes sense its an object due to the fact that all nodes are objects in the living document (Did I understood right?)

However the first part is what I would like to understand.

Here is the fiddle and my browser is FireFox 47.0 just in case.

DRP
  • 239
  • 4
  • 16

1 Answers1

1

I assign the id given to the DOM element

Nope, you assign the element itself. Assigning the id would be this:

var someVar = "noob";

Exposing DOM elements (those with id) as properties on window object (making them global identifiers) is a "feature" of the browsers. See this answer for way more details.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367