beanz Magazine

What are Keywords in Programming Languages?

Paul Downey on Flickr

Everyone knows the difference between saying, “Let’s eat, grandma!” and “Let’s eat grandma!” Computers don't.

In the first case, you’re hungry while in the second case likely you’re a cannibal. Humans understand the two different meanings caused by a single comma. Computers, however, can’t handle this kind of uncertainty. Computers don’t guess the meaning of code. They need clear directions and statements.

When you’re programming, you might have problems with the functions or variable names you use.

For example, in JavaScript the following code may look sensible but it’ll cause a problem when run:

function myFunction (this) {
    return this * this;

Why doesn’t this work? It’s because this is a keyword in JavaScript, also called a reserved word.
In JavaScript there are a number of names you can’t use such as this, for, function, or var as either the names of your functions or variables.

Every programming language has keywords like this. In Ruby you can’t name a variable yield or until. In Java you can’t name a function class. These keywords have been reserved by the designer of the programming language, and they almost always correspond to words that are important to the syntax of the language: for, if, else, then, var, yield, set, function or fun, etc.

That’s the “what” of keywords, but why do languages reserve words for themselves?

There are two basic reasons. First, it prevents programmers from using reserved words for functions or variables, which keeps the code clearer. Imagine having a conversation with a friend who used the word “is” to mean, say, “chicken”. They’d say to you, bewilderingly, “Do you want to come over tonight? We’re having is for dinner”. “What is is?” you’d ask cautiously. “Why, is is is!” they’d reply uselessly.
At this point you’d probably just throw your phone and be annoyed at them.

Similarly, if you could have a function called for or if or else then you’d have confusing code like

if (if(10) < 20){
else {

Maybe you can figure out what you mean if you write this, but surely no one else will, and in a month you probably won’t either.

The other reason for reserving words is that it makes life easier for the language creator.

One of the things that makes programming languages so different from human languages is that they don’t have ambiguity. Ambiguity is when the text that’s written can be read in multiple ways. It’s like the well worn joke about the difference between “let’s eat grandma” and “let’s eat, grandma”. “let’s eat grandma” is ambiguous because it could be read as “telling your grandmother that it’s time to eat” or “declaring your intention to eat your own grandmother”. Of course you and I know that’s probably just announcing to your grandmother that it’s time to eat, but we have to guess that.

You don’t want ambiguity in a programming language because it means at some point a guess is going to be made, and guessing is harder to code and harder still to get right. Instead, it’s much easier to restrict what you’re allowed to use as variable and function names.

So, in summary, every language has a small set of keywords that are important to the language. You can’t use keywords for names because that would cause ambiguity and make the code harder to understand for both a person reading it and the program executing it.

Learn More

Also In The April 2017 Issue

We all use fonts yet rarely notice they are designed. Here are some interesting details to help you notice fonts.

This iPad app is a creative tool kids can use to explore and record what they learn in school.

Smart software design makes it easy for you to learn how to use it without help.

A new version of a fun Mario-like game that teaches kids coding has been released. Learn HTML and save kittens!

Mark is a designer who also knows a lot about how to use technology to create design.

Design is about solving problems, from donuts to race cars, how we eat to what to wear in cold weather.

Everyone know the difference between saying, “Let’s eat, grandma!” and “Let’s eat grandma!” Computers don't.

The user interface often determines whether or not people can easily use your software.

State is an important concept in computer science as well as our everyday lives.

Links from the bottom of all the April 2017 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for April 2017.

Computers collect garbage the way humans do. Here's how they manage memory space.

Code reviews help programmers improve their code and learn more about the software they build.