dark mode light mode Search Menu
Search

What are Keywords in Programming Languages?

Paul Downey on Flickr

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;
}
console.log(myFunction(10));

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){
    if(3);
}
else {
    else(1);
}

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

https://www.programiz.com/c-programming/c-keywords-identifier

C keywords

https://www.educba.com/c-keywords/

Programming Visual Basic

https://www.oreilly.com/library/view/programming-visual-basic/0596000936/ch02s03.html

Reserved Word

https://en.wikipedia.org/wiki/Reserved_word”

C++ reserved words

http://www.umsl.edu/~lawtonb/224/oview2a.html

What it a reserved word?

https://www.computerhope.com/jargon/r/reseword.htm

Reserved words: Real Python

https://realpython.com/lessons/reserved-keywords/