JavaScript - Gotchas


Why should we avoid using variable named 'name'?

var name = "some value";

Although the word name is not a reserved word. Browsers (both Firefox 1.5 and Explorer 5) confuse this variable with The above code was inside a function so the browser should not confuse it with, but it does, so you should avoid it.

Why should we avoid giving a form field a name like "submit"?:

A name you should avoid is "submit" (for submit buttons and anything else). Browsers expose form elements as properties of the form element, by their name, in this case hiding native methods like submit(). Just don't use name="submit" and you're good.

Why should we avoid having comma at the end of the last property value in a JSON object?

The below JSON object will result in an error in IE:

var x = {
    "firstName": "Kevin",
    "lastName": "Dee",

Notice the comma after "Dee". That comma should not be there.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License