Despite its technical reputation, coding is a creative endeavor and offers a wealth of flexibility in solving problems. On the other hand, the language adheres to certain standards. Since a machine interprets code, you must follow certain protocols or risk non-working code. Even minor errors and typos can throw everything off and leave your code inoperable. Thankfully these are often small mistakes and quick changes. Today we will cover five of the most common errors.
Missing Semi-Colons
This error is the most common that even experienced coders make while they write scripts. Think of the colon as a period in JavaScript. Every time you assign a variable, or make a calculation it is like an individual thought. The semi-colon instructs the script on where the expression ends.
Unlike a language like Python, JavaScript does not recognize white space. If you forget a semi-colon, even after starting a new line, the script will not run correctly. In AdWords scripts themselves, the editor may misinterpret the error as well. Since there is no official closing, it may report issues on the wrong line number when there is a missing semi-colon.
Improper Capitalization
JavaScript is case sensitive. Your variable names must maintain consistent naming throughout the script. In terms of execution, “Bear” and “bear” are two completely different entities. They are never interchangeable.
The most common error you receive is a notification that you are attempting to use an undeclared variable. Alternatively, if you are using the same names with different capitalization (don’t do this!) you could inadvertently use the wrong variable in your functions.
The easiest way to avoid this problem is to use a standardized style. Most often this is accomplished by using camel case where the first letter of each word in the variable is capitalized, such as clicksToday. You could also use under scores, such as clicks_today. As long as you keep up the habit it will become natural and avoid many of these issues.
Improper Brackets
Brackets are similar to semi-colons, except they are used to encapsulate chunks of code, most often when building functions. When a script calls the function, everything within the brackets is executed. This has many benefits for both organization and code control but can present problems if you misplace them.
The two most common errors are missing and misplaced brackets. If you forget a bracket, the script may misinterpret the code within the function or may not run at all, giving you a missing bracket error. One of the benefits of the AdWords scripting interface is color-coding your brackets. If you click on a bracket that does not have a matching open or closing bracket, it will be highlighted in red. If it does have a closing bracket, it will be highlighted in green along with its partner.
Mixed Quotation Marks
JavaScript accepts both “ and ‘ in the code. The only catch is that you must be consistent in opening and closing them. If you use double quotes, close with double quotes and vice versa. It’s an easy mistake to make when the only difference is a shift key.
The other likely scenario is if you are trying to include quotes in a string variable. For example you want to insert quotation marks into ad copy; to do that simply switch between them. Open with single quotes, insert double quotes and the text, and then close with single quotes. Just like this, ‘ “This is a quote example” ‘. This will interpret the double quotes as part of the text, rather than part of the code.
While working in the editor just take a look at the coloring. The string variable should be a consistent color and different than the rest of the code. If everything is highlighted like a string, or not enough, you made an error with a quotation.
Variable Scope
Once you get beyond the basics of manipulating variables you’ll want to start playing with functions. Functions are confined pieces of codes that both organize code and help you with repeatable operations. For example rather than manually calculate ROAS or CPA you could define a function that you could simply reuse when needed, rather than write it out each time.
The easiest way to understand is that as the scope gets more specific, variables and data outside the scope are inherited but it does not necessarily work in reverse. If you initialize a variable inside a function, it will be private to that function. Not that you can’t use it somewhere else but you need to either return to another variable outside of the function. If you aren’t careful you may end up with results you are not expecting.