Tech Tuesday: Reserved Words

Just as a reminder, on Tech Tuesday I am currently going through what will be a pretty long cycle on programming.  The overview post sets out a series of questions to be answered.   The most recent posts on Variables, Constants and Literals and then on Data Types (Parts 1, 2 and 3) provide some of the basics for answering overview question 4 about how programs can refer to concepts as opposed just concrete things.  There is a lot more to say about this but first we will turn to overview question 5 about how to break what we want the computer to do down into smaller steps.

The post on Programming Languages introduced the idea that each language comes with its own set of “words” known as keywords or reserved words (there are some subtle differences that we may or may not get to).  These are our basic building blocs from which we put together longer expressions (think: sentences).  By combining expressions we form programs (think: a complete explanation how to do something).  The great news when you are just getting started is that in most programming languages there are relatively few reserved words altogether (30 - 100) and that you often need to know only a subset of these to get going.

In fact we have already seen and used some reserved words.  For instance, in Javascript, we saw “var” as in “var a = 5” was the way we defined a variable and gave it an initial value.  The “var” is a reserved word because it has a special meaning in the language and one that is “built into” the language.  The Javascript interpreter understands what to do when we say “var a = 5” without us needing to provide any further explanation.  It creates a new variable named a with initial value 5.  Now what would happen if we tried the following:

var var = 5;

What?  We are asking Javascript to define a variable named var.  See how this could be a problem? At a minimum it might lead to confusion for humans reading the program.  But it could even result in confusion for the computer.  Next time we use “var” in the program do we want it to mean our variable by that name or the built-in word for defining new variables?

In fact Javascript doesn’t like that line of code.  When we try to run it, we get an error as follows:

The Javascript interpreter is complaining that it was expecting a variable name after var.  That’s another (and somewhat cryptic) way of saying: you can’t use var as a variable name.  That’s why “var” is a reserved word.  It is reserved for a specific use by the language and not by you as you see fit.

Now you might also remember that a bunch of the Javascript examples used “alert()” as a way of popping up an alert box in the browser to display something.  For instance, alert(“Testing”) will pop up a box with the word “Testing” in it.  So what happens with the following bit of code?

alert("Testing");
var alert = 5;
alert("What happens here?");

If “alert” is a reserved word then we should get the same error we encountered before.  But that’s not what happens!  Instead we now see the following:

Javascript complains that alert is not a function *after* it has processed the var alert = 5.  So even though the “alert” came as something that was built into Javascript in the browser we were able to use it as a variable name and doing so removed its meaning as a function for opening an alert box.  Put differently, “alert” is not a reserved word for Javascript.

Now if you have paid close attention and also read the posts on syntax and semantics, you might have a number of objections to how Javascript handles all of this.  Such as, why wouldn’t it take into consideration where and how a word occurs?  It would seem to be straightforward to keep alert working as before when it is followed by “(” as in alert(“What happens here?”) and have it stand for the variable when used in something like say var more = alert + 10;  That’s an entirely valid point.  In fact as it turns out Javascript has a whole bunch of weirdness around reserved words  (scroll to end of the rather long page) — it is not the only language for which that is true though!

Next week we will learn about the largest group of reserved words in most languages - the so-called control structures, which tend to consist of words such as “if” and “while.”  These are there to let us influence the flow of program execution.

Posted: 26th June 2012Comments
Tags:  tech tuesday programming reserved words

Newer posts

Older posts

blog comments powered by Disqus
  1. continuations posted this

Newer posts

Older posts