| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Solve_JavaScript_problems | [Back] [Original] |
Get to know MDN better
The following links point to solutions to common problems you may encounter when writing JavaScript.
If your code doesn't work and/or the browser complains that something is undefined, check that you've spelt all your variable names, function names, etc. correctly.
Some common built-in browser functions that cause problems are:
| Correct | Wrong |
|---|---|
getElementsByTagName() |
getElementByTagName() |
getElementsByName() |
getElementByName() |
getElementsByClassName() |
getElementByClassName() |
getElementById() |
getElementsById() |
You need to make sure you don't place any semicolons incorrectly. For example:
| Correct | Wrong |
|---|---|
elem.style.color = 'red'; |
elem.style.color = 'red;' |
There are a number of things that can go wrong with functions.
One of the most common errors is to declare the function, but not call it anywhere. For example:
function myFunction() {
alert("This is my function.");
}
This code won't do anything unless you call it with the following statement:
myFunction();
Remember that functions have their own scope you can't access a variable value set inside a function from outside the function, unless you declared the variable globally (i.e., not inside any functions), or return the value from the function.
Remember also that when you return from a function, the JavaScript interpreter exits the function no code after the return statement will run.
In fact, some browsers (like Firefox) will give you an error message in the developer console if you have code after a return statement. Firefox gives you "unreachable code after return statement".
When you assign something normally in JavaScript, you use a single equals sign, e.g.:
const myNumber = 0;
With Objects, however, you need to take care to use the correct syntax. The object must be surrounded by curly braces, member names must be separated from their values using colons, and members must be separated by commas. For example:
const myObject = {
name: "Chris",
age: 38,
};
For more information on JavaScript debugging, see JavaScript debugging and error handling. Also, see Other common errors for a description of common errors.
this, in the context of an object?addEventListener() function do, and how do you use it?This page was last modified on Jul 9, 2026 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |