Solve common problems in your JavaScript code
The following links point to solutions to common problems you may encounter when writing JavaScript.
Common beginner's mistakes
Correct spelling and casing
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() |
Semicolon position
You need to make sure you don't place any semicolons incorrectly. For example:
Correct | Wrong |
---|---|
elem.style.color = 'red'; |
elem.style.color = 'red;' |
Functions
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:
js
function myFunction() {
alert("This is my function.");
}
This code won't do anything unless you call it with the following statement:
js
myFunction();
Function scope
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.
Running code after a return statement
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".
Object notation versus normal assignment
When you assign something normally in JavaScript, you use a single equals sign, e.g.:
js
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:
js
const myObject = {
name: "Chris",
age: 38,
};
Basic definitions
Basic use cases
General
Variables
Math
- What types of number do you have to deal with in web development?
- How do you do basic math in JavaScript?
- What is operator precedence, and how is it handled in JavaScript?
- How do you increment and decrement values in JavaScript?
- How do you compare values in JavaScript? (e.g. to see which one is bigger, or to see if one value is equal to another).
Strings
- How do you create a string in JavaScript?
- Do you have to use single quotes or double quotes?
- How do you escape characters in strings?
- How do you join strings together?
- Can you join strings and numbers together?
- How do you find the length of a string?
- How do you find what character is at a certain position in a string?
- How do you find and extract a specific substring from a string?
- How do you change the case of a string?
- How do you replace one specific substring with another?
Arrays
- How do you create an array?
- How do you access and modify the items in an array? (this includes multidimensional arrays)
- How do you find the length of an array?
- How do you add items to an array?
- How do you remove items from an array?
- How do you split a string into array items, or join array items into a string?
Debugging JavaScript
- What are the basic types of error?
- What are browser developer tools, and how do you access them?
- How do you log a value to the JavaScript console?
- How do you use breakpoints and other JavaScript debugging features?
For more information on JavaScript debugging, see Handling common JavaScript problems. Also, see Other common errors for a description of common errors.
Making decisions in code
- How do you execute different blocks of code, depending on a variable's value or other condition?
- How do you use if ...else statements?
- How do you nest one decision block inside another?
- How do you use AND, OR, and NOT operators in JavaScript?
- How do you conveniently handle a large number of choices for one condition?
- How do you use a ternary operator to make a quick choice between two options based on a true or false test?
Looping/iteration
Intermediate use cases
Functions
- How do you find functions in the browser?
- What is the difference between a function and a method?
- How do you create your own functions?
- How do you run (call, or invoke) a function?
- What is an anonymous function?
- How do you specify parameters (or arguments) when invoking a function?
- What is function scope?
- What are return values, and how do you use them?
Objects
- How do you create an object?
- What is dot notation?
- What is bracket notation?
- How do you get and set the methods and properties of an object?
- What is
this
, in the context of an object? - What is object-oriented programming?
- What are constructors and instances, and how do you create them?
- What different ways are there to create objects in JavaScript?
JSON
Events
- What are event handlers and how do you use them?
- What are inline event handlers?
- What does the
addEventListener()
function do, and how do you use it? - Which mechanism should I use to add event code to my web pages?
- What are event objects, and how do you use them?
- How do you prevent default event behavior?
- How do events fire on nested elements? (event propagation, also related — event bubbling and capturing)
- What is event delegation, and how does it work?