Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Basic JavaScript

JS Tutorial JS Introduction JS Where To JS Output

JS Syntax

JS Syntax JS Statements JS Comments JS Variables JS Let JS Const JS Types

JS Operators

JS Operators JS Arithmetic JS Assignment JS Comparisons JS Conditional JS If JS If Else JS Ternary JS Switch JS Booleans JS Logical

JS Loops

JS Loops JS Loop for JS Loop while JS Break JS Continue JS Control Flow

JS Strings

JS Strings JS String Templates JS String Methods JS String Search JS String Reference

JS Numbers

JS Numbers JS Number Methods JS Number Properties JS Number Reference JS Bitwise JS BigInt

JS Functions

Function Path Function Intro Function Invocation Function Parameters Function Returns Function Arguments Function Expressions Function Arrow Function Quiz

JS Objects

Object Path Object Intro Object Properties Object Methods Object this Object Display Object Constructors

JS Scope

JS Scope JS Code Blocks JS Hoisting JS Strict Mode

JS Dates

JS Dates JS Date Formats JS Date Get JS Date Set JS Date Methods

JS Arrays

JS Arrays JS Array Methods JS Array Search JS Array Sort JS Array Iterations JS Array Reference JS Array Const

JS Sets

JS Sets JS Set Methods JS Set Logic JS Set WeakSet JS Set Reference

JS Maps

JS Maps JS Map Methods JS Map WeakMap JS Map Reference

JS Iterations

JS Loops JS Iterables JS Iterators JS Generators

JS Math

JS Math JS Math Reference JS Math Random

JS RexExp

JS RegExp JS RegExp Flags JS RegExp Classes JS RegExp Metachars JS RegExp Assertions JS RegExp Quantifiers JS RegExp Patterns JS RegExp Objects JS RegExp Methods

JS Data Types

JS Destructuring JS Data Types JS Primitive Data JS Object Types JS typeof JS toString JS Type Conversion

JS Errors

JS Errors Intro JS Errors Silent JS Error Statements JS Error Object

JS Debugging

Debugging Intro Debugging Console Debugging Breakpoints Debugging Errors Debugging Async Debugging Reference

JS Conventions

JS Style Guide JS Best Practices JS Mistakes JS Performance

JS References

JS Statements JS Reserved Keywords JS Operators JS Precedence

JS Versions

JS 2026 JS 2025 JS 2024 JS 2023 JS 2022 JS 2021 JS 2020 JS 2019 JS 2018 JS 2017 JS 2016 JS Versions JS 2015 (ES6) JS 2009 (ES5) JS 1999 (ES3) JS IE / Edge JS History

JS HTML

JS HTML DOM JS Events JS Projects New

JS Advanced

JS Temporal  New JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Debugging

Debugging for Beginners

Many beginners quit because they cannot debug.

This page shows you how to find out why code does not work.

Debugging means finding and fixing mistakes (bugs) in your code.

Bugs are normal. The skill is learning how to locate them quickly.

Why Code Does Not Work?

Programming code might contain syntax errors, or logical errors.

Many of these errors are difficult to diagnose.

When code fails, beginners often guess what is wrong.

Debugging is the opposite: you check facts.

A good debugging habit is: ReadReproduceReduceFix

  • Read the error
  • Reproduce the problem
  • Reduce to a small example
  • Then fix it.

Often, when programming code contains errors, nothing will happen. There are no error messages, and you will get no indications where to search for errors.

Searching for (and fixing) errors in programming code is called code debugging.


Errors can and will happen, every time you write computer code.


JavaScript Debuggers

Debugging is not easy. But fortunately, all modern browsers have a built-in JavaScript debugger.

Built-in debuggers can be turned on and off, forcing errors to be reported to the user.

With a debugger, you can also set breakpoints (places where code execution can be stopped), and examine variables while the code is executing.


Did You Know?

Debugging is the process of testing, finding, and reducing bugs (errors) in computer programs.

The first known computer bug was a real bug (an insect) stuck in the electronics.


Step 1: Look in the Console

The browser console shows errors and messages from JavaScript.

If your code "does nothing", the console often tells you why.

Normally (otherwise follow the steps at the bottom of this page), you activate debugging in your browser with the F12 key, and select Console in the debugger menu.

If you do only one thing: always check the console when something fails.


Step 2: Use console.log()

In JavaScript console.log() prints values to the console.

This helps you to see what your code is doing.

Examples

Print a message:

<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>

<script>
console.log("Hello!");
</script>

</body>
</html>
Try it Yourself »

Print variables to find what is wrong:

let price = 50;
let quantity = 3;
let total = price * quantity;

console.log("Total:", total);
Try it Yourself »

Tip:

Log the value before and after the line you suspect.

That can tell you where things start going wrong.


Step 3: Confirm Your Assumptions

Many bugs happen because you assume a value is something, but it is not.

Check the value.

Check the type.

Example

Numbers and strings behave differently:

let x = 5;
let y = "5";

console.log(x + y);  // 55 (string!)
console.log(x + Number(y));  // 10 (number)
Try it Yourself »

Reading Error Messages (Beginner Friendly)

Error messages look scary, but they usually mean one of a few common things.

ReferenceError

Means: This name does not exist.

Often a misspelling or variable not declared.

Example

console.log(myValue);
// ReferenceError: myValue is not defined
Try it Yourself »

TypeError

Means: You tried to use a value in an impossible way.

Often undefined or null.

Example

let x;
console.log(x.length);
// TypeError: Cannot read properties of undefined
Try it Yourself »

Note

In the console window, the error usually includes a line number.

Click it in the console to jump to the exact line.


A Simple Debugging Checklist

  • Check the console for errors
  • Read the error message carefully
  • Log values with console.log()
  • Reduce the problem to a small example
  • Fix one thing at a time.

Common Beginner Mistakes

Examples

Mistake: Using = instead of == or ===

let x = 10;
if (x = 5) {
  console.log("This runs");
}
Try it Yourself »

Fix: Use comparison:

let x = 10;
if (x === 5) {
  console.log("This runs only if x is 5");
}
Try it Yourself »
If your if statement behaves strangely, check if you accidentally used =.


Major Browsers' Debugging Tools

You can activate debugging in your browser with F12, and select Console in the debugger menu.

Otherwise follow these steps:

Chrome

  • Open the browser.
  • From the menu, select "More tools".
  • From tools, choose "Developer tools".
  • Finally, select Console.

Firefox

  • Open the browser.
  • From the menu, select "Web Developer".
  • Finally, select "Web Console".

Edge

  • Open the browser.
  • From the menu, select "Developer Tools".
  • Finally, select "Console".

Opera

  • Open the browser.
  • From the menu, select "Developer".
  • From "Developer", select "Developer tools".
  • Finally, select "Console".

Safari

  • Go to Safari, Preferences, Advanced in the main menu.
  • Check "Enable Show Develop menu in menu bar".
  • When the new option "Develop" appears in the menu:
    Choose "Show Error Console".

Example

<script>
let a = 5;
let b = 6;
let c = a + b;

console.log(c);
Try it Yourself »



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->