Creating & Manipulating Variables In AdWords Scripts

By Jacob Fairclough | @SomeSecretJake | Senior Account Analyst at Hanapin Marketing

One of the biggest things missing in the AdWords scripting sphere is a “here is what makes up a script”. There are lots of cool snippets code out there that does all kinds of voodoo. Yet I am most often am asked, “So how do I get to that point?”

The good news is, no you don’t need a computer science degree. Think of a programming language like any other language, there are tons of people who speak them without degrees in the language. One of the first ways you learn a language is by learning its basic components. Today we’ll start with variables, the most essential unit of code.

What is a Variable?

Variables are one of the primary units in any code. As the name suggests, they are in fact variable. You can enter any information you want and store it in a named variable.

If it helps, think of variables like you would think of variables in math. It is only a chosen symbol that represents another value. Not all variables are the same though, there are multiple types and each has it’s own purpose and usages.

This sounds complicated but don’t worry, you’ll get to the end of this and realize how easy it really is. If you can understand match types, you can understand variables. Let’s get started!

Declaring a Variable

If you want to use a variable, you have to make space for it. If you fail to do this, there won’t be anywhere to store data. Before you use it, you have to claim your space. You do this by initializing the variable. Which is as easy as,

var myVariable = ;

The var establishes the variable, after that you enter the name. You can name it whatever you want, outside of a few exceptions for reserved words. You need to be consistent though as capitalization counts. myVariable is not the same as myvariable.

You can name variables almost anything you want as well as store anything you want inside of them. One of the best practices when writing code is to be very clear with your variable names. Trust me, it’ll make it easier when you look back at old code.

Then the = assigns a value to the variable. It sets the variable as equal to whatever follows it, to the right. You’ll notice in the example above that we don’t have anything there though. This does not break anything but it also means if I try and use myVariable, I can’t do much with it since it was never assigned a value.

To finish the declaration, the ; ends the expression. It is a poor analogy but the ; is like a period in a sentence. Just tag it on the end before moving to the next part.

So now you have a variable, but what do you do with it? Well, now you assign something to it.


One of the easiest and most common usages for variables is storing numerical values. To do this simply enter the number you’d like to store after the assignment operator, =, and you are good to go.

var myNumber = 10;

var yourNumber = 25;

You can also assign the value by using other variables.

ourNumber = myNumber + yourNumber;

Easy as pie, right? So why would we want to store a number rather than write it directly? Well, variables hold the values for you so you can use them at a later point without having to remember what they were or retype them. They are also useful for holding constants and values you are going to use repeatedly. Don’t risk mistyping it in the code when you can store the value in one place.


We can store numbers now, but what about text. Without the ability to store text, you won’t be able to build alerts, messages, or write names. All the basics apply, you just have to set a variable as text. You do this by inserting the text inside of quotation marks, similar to phrase match.

var greeting = “Hello!”;

var jobTitle = “Account Manager”;

You are free to use “ or ‘, as long as you are consistent. For example ‘text” won’t work. The string ends with the second quotation mark, so if you ever need to store something in quotes, simply alternate between the single and double quotation marks.

var aQuote = ‘ ”To Be Or Not To Be” ‘;


Arrays operate similar to ordered lists. Arrays are used to store multiple values; these could be any type of variable too. Simply insert the values in between brackets and separate each with a comma.

var matchTypes = [“Broad”, “Phrase”, “Exact”];

var birds = [“Albatross”, “Seagull”, “Ostrich”, “Penguin”];

var numbers = [1,2,3,4,5];

So we have a variable holding a bunch of values, how do you access them again? You access the values of an array through the index. Simply type variable followed by the index you’d like to retrieve.

matchTypes[1] would reference “Phrase”. Don’t worry you read that correctly. It’ll trip you up the first few times but JavaScript (and most coding languages) start the index at 0, rather than 1.

If you really want to blow your mind, Arrays can contain other arrays.

var mindBlown = [[1,2,3], [“a”, “b”, “c”]];

To retrieve the “a”, I would use two indices, starting from the outside and working in. First I’d select the second array with the letters, and the first value within that array.


These will come in handy when you learn other concepts such as loops and iterators but if you aren’t that far yet, it’ll be helpful to know what these are when you see them.


There is one last variable type we will cover today. That It’ll only be a brief overview, as objects themselves deserve their own posts. Objects are variables with a group of properties.

In this way, objects are similar to arrays in that they contain multiple items. Rather than being indexed though, an object has properties. You can think of these as variables inside the object.

To declare an object, use {}. To declare the properties you simply insert the property name, a colon, and the value.

propertyName : value

To add additional properties, simply enter a comma after the value and create a new property.

To use a PPC example,

var keyword = {

text: “keyword”,

maxCPC: 1.5;


Other common examples are phone book entries or car models. Each unique entry will have it’s own set of properties associated with it. An object allows you to store all the properties together, under one variable. But what if I want to only access the CPC of the keyword above?

You can do this in two ways,



Here is a little secret to pique your curiosity, at its heart JavaScript is made up of objects. If you’ve looked at a few script examples, the keyword.maxCPC probably looks familiar. This is because all those entities you interact with in AdWords are made up of collections of objects, containing all the metrics and settings as properties.


We covered quite a bit of material today. Hopefully this helps you gain a firmer grasp on variables. Did you find any areas that require further clarification? Feel free to chime in with any questions. I can either answer them in the comments or use it for new material in this column.

New Sessions. New Networking. An All New Experience.

When we get feedback on ways to make Hero Conf more relevant, more actionable, and more meaningful, we take it pretty seriously. It's our most powerful tool in creating an event we believe helps our industry thrive. And we've used that information to build our...