Why Won't My Code Work!? - The Beginner's Guide To Fixing ...
Maybe your like
If your JavaScript code isn't working, use this handy checklist to find (and fix) the bugs.
25 November 2014 ·
It’s a normal part of writing code to get unexpected results from time to time. For a JavaScript beginner, though, it can be hard to know where to find your mistakes so that you can fix them.
So, if your code isn’t working and you’re not sure where the error might be, use this checklist to help you find and fix it.
Step 1: Check your files
-
Look in your main website/project folder - do you have an index.html file and a scripts.js file sitting next to each other?

-
Does your index.html file contain a reference to the external scripts.js file? Is the reference correct (no spelling errors, etc)?

-
Are you definitely viewing the correct index.html file in the browser? Take a look at the browser’s address bar to find out
Step 2: Open the browser’s console
-
Remember: to open a browser’s console to examine the code behind the page you’re viewing, right-click anywhere and choose ‘Inspect element’ - then look for the ‘Console’ tab and click on it

-
Are there any errors shown there? e.g. ‘undefined’ variables - at this stage, it’s a good idea to double-check that you’ve spelled all variable and function names correctly in your code; remember that 'myVar' is not the same as 'myvar'

-
Add some console.log() statements to your code, if you haven’t already, to let you know whether the various steps are working (e.g. log any variables that you create, and log helpful messages to check that any if-statements and loops are working correctly)
-
Don’t forget a console.log('executing scripts.js'); message at the top of your JS file to make sure that it is running
Step 3: Check your code on JSHint.com
- This tool will help you to find more syntax errors, as well as potential bugs such as implicit type conversion, leaking variables and various unsafe coding practices

Step 4: Start from the top
-
Comment out all of your code using multi-line comment tags: /* */
-
Move the opening comment tag downwards to un-comment a small section of code at the top, then check whether this code runs correctly in the browser (adding console.log() statements to test things if needed)

-
If the first small section of code works, un-comment the next small section to test it in the same way
-
Eventually, you will discover the section(s) of your code that are causing the problem - here, triple-check your syntax and use extra console.log() statements to examine every single piece of code
Step 5: Emergency procedures
If you still haven’t found any explanation for your code not working, you may want to consider creating a new scripts file (e.g. scripts2.js) to recreate certain sections of your code - it’s rare, but sometimes there can be a bug in the brower or text editor.
And if you’re still stuck after this, you might want to head over to StackOverflow’s JavaScript section and browse or search for solutions.
If there’s an error in your browser console or on JSHint.com, copy and paste the error into Google or StackOverflow’s search box to see who else has struggled with it before and how they fixed it.
Most questions about programming languages that have been around for a while, like JavaScript, have been asked already, so you’re highly likely to discover the solution to the issue you’re having if you search online.
Please post in the comments if you have your own tips and tricks for finding and fixing JavaScript bugs!
- JavaScript
Share:
Join the discussion by adding your comments below:
Related Posts
1 June 2017 PhoneGap: An Introduction
Did you know you can use your HTML, CSS and JavaScript skills to build apps for phones and tablets?
1 June 2017 How to Make Learning JavaScript Easier
If JavaScript is your first programming language, it can take a little getting used to. These tips will help.
1 June 2017 Add a 'Like' Button to Your Blogposts Using This Simple JavaScript Technique
A quick & handy intro to using JavaScript to make your website more interactive and engaging.
1 June 2017 The Best JavaScript Libraries and their Most Powerful Features
Once you've become familiar with what plain JavaScript offers, you'll find it useful to look at certain libraries for some of your projects.
Related Courses
You might be interested in these related courses:
JavaScript for Beginners
Popular Courses
-
AI and Deep Learning with Python
-
Computer Basics and IT for Beginners
-
Data Analysis with Python
-
Data Science and Machine Learning with Python
-
Microsoft Excel for Beginners
-
Microsoft Power BI for Beginners
-
Power BI Intermediate to Advanced
-
Python Intermediate to Advanced
-
Python Programming for Beginners
-
SQL for Beginners
-
SQL Intermediate to Advanced
-
Web Design for Beginners
Useful Links
- Courses
- Corporate Training
- Timetable
- Blogs
- Videos
- Training Venues
- Careers
- Become an Affiliate
- IT Skills
- FAQs
- About Us
- Register
- Contact Us
- Terms and Conditions
- Privacy Policy
Share this page now!
What we do?
At London Academy of IT, we provide instructor-led online and in-person IT training in Data Analytics, SQL, Python, Power BI, and more. Our cutting-edge courses are designed to boost performance and enhance employability, providing the competitive edge employers look for.
Our Contacts
London Academy of IT 64 Broadway Stratford London E15 1NT United Kingdom
Call us: +44 (0)208 432 6218 WhatsApp: +44 0749 461 6045
Regional Training
USA | UAE | Ireland UTC British Time (UK, Ireland, Iceland) Central European Time (France, Germany, Sweden) Eastern European Time (Finland, Cyprus) Eastern Time (New York, Toronto, Montreal) Central Time (Chicago, Houston, Winnipeg) Mountain Time (Calgary, Denver, Edmonton) Mountain Time (Phoenix) Pacific Time (Los Angeles, Seattle, Vancouver) Singapore Time Arabic Standard Time (Qatar, Saudi Arabia) Gulf Standard Time (UAE, Oman) Australian Eastern Time (Sydney, Melbourne) Western Australia Time (Perth) New Zealand Time China Standard Time (China, Taiwan, Hong Kong)Tag » Code . Wont Work
-
"code ." Is Not Working In On The Command Line For Visual Studio ...
-
24+ Reasons Code Doesn't Work & How To Fix It - WP SITES
-
Why Doesn't My Code Work?
-
Why Doesn't My Code Work? - Quora
-
9 Reasons Why Your QR Code Is Not Working - Beaconstac Blog
-
My Code Won't Work! How Do I Fix It?
-
That Code Didn't Work. Check The Code And Try Again.
-
What To Do When Your Code Doesn't Work? - Medium
-
Troubleshoot Visual Studio Code Integrated Terminal Launch Failures
-
Troubleshoot Gift Card And Code Issues - Xbox Support
-
How To Install Visual Studio Code's `code` Command Permanently?
-
Google Authenticator Codes Stopped Working? How To Fix This Issue
-
Why Doesn't My Verification Code Work? - Circle Help Center