1

Before you quickly post with "RTFM" or with a bunch of links I have visited, I am fully aware of the documented reserved variable names not to use.

The best lists I could find are here:

http://es5.github.com/x7.html#x7.6.1.1 and http://www.javascripter.net/faq/reserved.htm

What I am asking for are the variable names that are invalid on 1 or 2 browsers only.

For example I can define print but this will cause an error as print is already defined on the global scope window. Why isn't this documented as a reserve keyword?

Is there a big list of variable names to avoid?

iambriansreed
  • 21,935
  • 6
  • 63
  • 79
  • 1
    You can set `window.print = "Hello, world!"` whenever you want. But anyway, what are trying to do? Just to avoid syntax errors? – MaxArt Jun 07 '12 at 12:33
  • 3
    The javascript language is independent from the host objects that appear in browsers. `print` is a method on the `window` object in a browser. It is not part of the javascript language. Because browsers make all properties of the `window` object available at the global scope in the javascript context, these host methods or properties can impact javascript code. – jfriend00 Jun 07 '12 at 12:34
  • There is not a lot of relation but look this website : http://mothereff.in/js-variables – Alexandre Khoury Jun 07 '12 at 12:37
  • @MaxArt Yes... avoiding syntax errors is the point. – iambriansreed Jun 07 '12 at 12:38
  • @Mageek That's nice but only speaks to the reserved keywords already referenced in the links above. Thanks though. – iambriansreed Jun 07 '12 at 12:40
  • @jfriend00 That's informative to those who don't know but how is it relevant to the questions I asked. Thanks anyways. – iambriansreed Jun 07 '12 at 12:42
  • @iambriansreed - you asked about the global symbol `print` and I was explaining why you have a conflict and yet why `print` is not a reserved word in the javascript language definition. You will need to be looking for lists of properties on the window object in all browsers. Since many browsers automatically make global objects for all objects with an id value, this list is infinite based on the HTML content. – jfriend00 Jun 07 '12 at 12:56
  • So if I don't get an answer then I plan on printing a list of all default global variables in all main browsers, maybe using browser stack. That is basically what I am looking for. – iambriansreed Jun 07 '12 at 13:25

2 Answers2

2

What I am asking for are the variable names that are invalid on 1 or 2 browsers only.

If any such words exist, that browser would be non–compliant with ECMA-262. Only the reserved words in ECMA-262 are "invalid variable names" (provided the name is otherwise compliant with the criteria for variable names, such as allowed characters).

Why isn't [print] documented as a reserve keyword?

Reserved words can only be defined by standards that have a concept of reserved word, the only standard relevant to scripting DOMs in browsers that has that concept is ECMA-262. And it doesn't list print as a reserved word, so it isn't one.

There are various DOM standards that define host objects and their properties, the closest they might get to the concept of reserved word is that of a read–only property, or one that is not writeable. window.print is defined in HTML5, which doesn't define it as not being writable or that it should throw errors if assigned to or attempts are made to modify it. So it doesn't exhibit any behaviour approaching that of a reserved word.

Is there a big list of variable names to avoid?

No, because there aren't any. However, host objects (like window) have default properties that are writeable, you should avoid overwriting them. They should be listed in various places, like the HTML5 specification and browser vendor documentation.

e.g. the following links for the window object:

  1. W3C HTML5 window object: http://www.w3.org/TR/html5/browsers.html#the-window-object
  2. MDN window object: https://developer.mozilla.org/en/DOM/window
  3. MSDN window object: http://msdn.microsoft.com/en-us/library/ms535873(v=vs.85).aspx

In addition, there is a simple for..in loop (per Berji's answer) to discover the enumerable properties at a particular time, however that may not be a comprehensive list of all possible property names and will include user defined properties along with default browser properties without distinction.

It is a better strategy to adopt a naming convention that avoids likely property names and minimise the use of global variables (i.e. user defined properties of the window object).

RobG
  • 142,382
  • 31
  • 172
  • 209
  • -1 Argh. Please reread the sentences above which end in a question mark. – iambriansreed Jun 07 '12 at 12:48
  • The abbreviated version: the only standard relating to browser scripting that has a concept of "reserved word" is ECMA-262. The others don't have the concept, so they don't have reserved words. – RobG Jun 07 '12 at 12:53
  • 1
    I think the OP wants a list of variable names that different browsers don't like, not according to ECMAScript, but what the browsers actually don't like. Including the non-compliant stuff. – zatatatata Jun 07 '12 at 23:32
  • Thanks @VahurRoosimaa. Contributors like you and the answer I accepted renew my hope in SO. – iambriansreed Jun 08 '12 at 00:32
  • -1 again. @RobG unfortunately some of us have to support non-compliant browsers. The fact that you answered "No..." there aren't any variables I should avoid is flat out incorrect. – iambriansreed Jun 08 '12 at 00:32
  • @iambriansreed—Ok, so what browser in use has variable names you can't use that aren't in the reserved word list? I spend most of my time here trying to get people to support browsers more than 12 months old, I don't need reminding of the need to support non—standard browsers. I don't know of any in popular use that have non–trivial non–compliance with EMCA–262. – RobG Jun 08 '12 at 02:57
  • @iambriansreed—still waiting for examples to prove I'm wrong. – RobG Jun 09 '12 at 05:11
  • `var print = 'Hello!'; alert(print); window.print();` I should Avoid using `print`. This is one variable in a long list. – iambriansreed Jun 09 '12 at 13:51
  • An unfortunate consequence of browsers making *window* a pseudonym for the global object is that all global variables are made properties of the window object and *vice versa*. If you want to discover all the properties of all the window objects in every browser, then do as Bergi says, but they aren't necessarily all enumerable. Nor are they reserved words (see above). Note that in IE all element ID and NAME values are property names of the global/window object too. – RobG Jun 09 '12 at 15:46
  • I guess you never read my question. You are basically repeating many of the points I made 2 days ago after I repeated the example I gave in my question. – iambriansreed Jun 09 '12 at 20:48
  • I did read your question, you were asking why default properties of host objects weren't listed as (ECMAScript) reserved words. I've answered that (and tried to clear up your confusion over variable names, property names and reserved words, apparently unsucessfully). It seems your question was really about what host object property names you should avoid, but you haven't updated your question. So my answer remains for those similarly confused about the terms, though I'll update my final paragraph. – RobG Jun 12 '12 at 02:31
1

If you want a big list of browser-defined variables to extend the official list of reserved keywords, type the following into your console on an empty page (about:blank):

>>> Object.getOwnPropertyNames(window)
>>> Object.getOwnPropertyNames(Object.getPrototypeOf(window)) // sometimes not Object

which will yield an Array of strings you definitely should not use. It includes "print", too.

Yet it will be browser-dependent, because some do not implement all the cool HTML5-drafted stuff that uses the Window interface; e.g. you won't find "Worker" in IE9, "openDatabase" in FF and so on. Also, it might not list legacy properties like "onload", altough you will be able to to get a property descriptor for it (and "onload" in window === true).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I will run jsFiddle on browserstack with a simple HTML5 with a single `
    ` and `JSON.stringify` the results from those two commands. I will compile the list from all the browsers and that will give me the end result.
    – iambriansreed Jun 08 '12 at 00:29
  • Could you publish that list somewhere, then? I'd be glad to see it :-) – Bergi Jun 08 '12 at 00:34
  • I will blog it and add the link here. Thanks! – iambriansreed Jun 08 '12 at 00:40
  • Before you start, you might add a fallback with a for-in-loop for older browsers not supporting `Object.getOwnPropertyNames`... – Bergi Jun 08 '12 at 00:42
  • 1
    Host objects do not have to support native methods, so you can't be guaranteed that the above will work at all, it may even throw errors (ane certainly will in browsers that don't support `Object.getOwnProperties`) – RobG Jun 09 '12 at 05:13