Blog Post

A bookshelf displaying the cover of Intro to JavaScript

Excerpt – An Introduction to Programming in JavaScript: Stomping Zombies with Variables, Loops, Functions and More

Buckets and Buckets of Zombie Guts

One of the first things to understand is a concept called variables. Variables are like buckets you store things in. You can put water in a bucket or your lunch or a mannequin head or the rookie cards of the 2037 Los Angeles Zombie-Dodgers Human Resistance Cell. You can put just about anything in a bucket.

There are also lots of different JavaScripty types of things you can put in a bucket, such as:

Numbers: These are what you think they are.

Strings of Letters (Strings): These are actually more than just letters—they are any text or character that you want to treat as a single thing. Enclose strings in quotation marks (both single and double quotes work in JavaScript) and can include any valid character. Examples of strings are:

“Hello Post-Apocalyptic World”


“Don’t look at me, Norm. The zombie pulled your lunch out of the fridge and ate it.”


“I know you labeled it. The zombie didn’t care or couldn’t read.”


“Only one way to find out. He’s over there eating 7 other lunches.”





So you can have number characters in a string too, but they’ll behave as strings and not as numbers—so you can’t add or multiply them without converting them back to numbers. If something is in quotes, it’s a string.

Boolean: The value is true or false and can’t be anything else. This makes it great for using it like a switch to turn something on or off.

There are a few more types of variables in JavaScript, but this is enough to get us started.

Buckets of Type Z

Some buckets can only hold one type of thing. For instance, a bucket with a hole in the bottom wouldn’t hold water or the soda from the last working soda fountain in North America, but it could be great for holding books or large rocks or the personal effects of your zombified roommate. Similarly, variables often only hold one type of thing.

In some programming languages, putting the wrong type of content in the wrong bucket can cause errors, but JavaScript is forgiving. The positive side of that is that JavaScript just handles a lot of stuff without complaining. The negative is that if it messes up or handles something different from what you expect, you can get weird results or programming bugs that are hard to track down.

Night of the Living Tip:

One common place these types of bugs happen is when you try to add a number (2) and a string that looks like a number (“2”). In JavaScript, 2 + “2” is “22”. Since one of these is a string, it casts/treats the other as a string too and puts them together or concatenates them. (This can be very useful when you need to construct a message for the user.) Thus, be careful that you are adding numbers with numbers and not strings.

Variable Bucket Contents

These buckets we’ve been talking about are called variables. They’re called variables because they are…ahem…variable. You can put something in them, and, during your program, you can take that out and look at it or take it out and do something with it or take it out and put something else in. You can do that any number of times.

Before we can use these buckets/variables to beat back the zombie hordes, we have to name them. We can name a variable just about anything we want. However, we can only use letters, numbers, and underscores. We can’t use any other characters, and the first character can’t be a number—i.e., 7zombies is invalid, but sevenzombies or _7zombies or zombies_numbering_7 would all work just fine.

Also, variables are case sensitive, so a variable of zombie is different from a variable of Zombie, which is also different from a variable named ZOMBIE, which is different from zOmBiE—so watch which case you use, and use that case throughout your code.

Want to keep reading? Pick up An Introduction to Programming in JavaScript: Stomping Zombies with Variables, Loops, Functions and More