MDN will be in maintenance mode on Thursday September 21st, starting at 10 AM Pacific / 5 PM UTC, for about 1 hour.

Learn web development

Solve common problems in your JavaScript code

이 문서는 아직 자원 봉사자들이 한국어로 번역하지 않았습니다. 참여해서 번역을 마치도록 도와 주세요!
English (US)의 문서도 읽어보세요.

The following links point to solutions to common everyday problems you'll need to fix in order to get your JavaScript code to run correctly.

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()

Semi-colon position

You need to make sure you don't place any semi-colons incorrectly. For example:

Correct Wrong = 'red'; = '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, for example with


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 out of the function.

Running code after a return statement

Remember also that when you return a value out of a function, the JavaScript interpreter exits the function — no code declared 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.:

var myNumber = 0;

This doesn't work in Objects, however — with objects you need to separate member names from their values using colons, and separate each member with a comma, for example:

var myObject = {
  name : 'Chris',
  age : 38

Basic definitions

Basic use cases


Debugging JavaScript

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


Intermediate use cases

문서 태그 및 공헌자

 이 페이지의 공헌자: chrisdavidmills, tedmarynicz, adekoyejo
 최종 변경: chrisdavidmills,