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

Keywords and identifiers

C keywords

Programming Visual Basic

Reserved Word”

C++ reserved words

What it a reserved word?

Reserved words: Real Python

Also In The October 2021 Issue

30+ ideas for STEAM-theme gifts for kids of all ages!

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

Join us as we weave together how looms and binary data are comparable!

Time for lesson two on how to make a fairy tale in Scratch!

Meet a 'Colossus' in the world of early encryption!

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

Looking to put your programming skills to the test? It may be time to join an open-source project!

Exploring the relationship between operators, negation, and exclamation points!

What is a G and why do we have 5 of them now? Find out through this quick history of cellular data!

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

A collection of fun and inspiring stories from October 2021.