Misspelling – Computers have gotten better at finding spelling errors but they can still happen! I have called misspelled functions before and invoked variables with incorrect spelling. When nothing happens when I execute the program, I let out a sigh and start looking through my code only to find that I switched two letters in my spelling of a variable.
Quotation marks inside of strings - This isn't exactly an error by itself, but it's tricky (and is why I like using double-quotes for strings). The general rule here is: Use opposite types of quotation marks inside a string that you use to surround the string. For example, are you using single-quotes around your string? Then use double-quotes if you need to quote anything inside your string.
No closing parenthesis/brackets – Similarly, when you use opening parentheses or brackets, you have to use closing parentheses, otherwise you’ll return an error in your code. Again, I don’t struggle too much with this since I’ve built a habit to automatically code in closing brackets/parenthesis when I write. Also, a lot of code editors will automatically add these. One thing to watch out for (which gets me a lot) is typing a closing bracket/parenthesis after the code editor adds one. Then you’ll have two! And an error on top!
With these errors comes some handy tips that I’ve picked up to help you avoid errors and troubleshoot when they appear.
Secondly, to return results in the console, you can use the handy
console.log() function, where the output you want to appear will go between the ( ). Alternatively, if the JS console is full of a bunch of stuff and you need to clear it out to start fresh, you can use the
console.clear() function to do just that. Give it a try!
Lastly, and one of my favorites, is the typeof operator. You can type
typeof 5 for example to figure out what data type the 5 is (spoiler: it’s a number). Another example would be
typeof "Hello there!" which is a string.
Hopefully, this helps you out if you are starting to code. It’s a fun adventure! If you have any other common errors you experience and ways to solve them, drop them in the comments below.