CHAPTER

A More Complete Programming Language

TIME LIMIT: 2 HOURS

You, using a pen and paper, can do anything a computer can; you just can’t do those things billions of times per second. - Paul Ford

Before we get into the nuts and bolts of JavaScript, let’s get a little more computer science-y for a minute: What is a programming language?

A programming language is any language used to create a program.

That’s it. That’s all there is to it. HTML, CSS, and JavaScript are all programming languages. If you’re talking to a recruiter and they ask what languages you know, you can comfortably include “HTML, CSS, JS” as part of whatever list you rattle off.

However, many programmers will contend that HTML and CSS are markup and styling languages, respectively, and that they therefore do not count as a real programming language (the way JavaScript does).

At the Front End PDX meetup.

What these programmers are generally driving at with this academic contention is that HTML and CSS are not Turing complete.

Turing Completeness

Born in 1912, Alan Turing was the grandaddy of modern-day computer science. A stunningly brilliant mind, he was the primary cryptanalyst (code-breaker) for the Allies during World War 2 and is credited with shortening that war by several years and tens of millions of lives.* In 1936 BC (before computers), he published a super-famous paper called “On Computable Numbers, with an Application to the Entscheidungsproblem.” This paper described a “universal computing machine”, which is a machine that can perform any conceivable mathematical computation that can be described. Of course, the machine also has to have enough memory to store the various bits of data it needs to make the described calculation.

This is worth repeating: A universal computing machine is any machine which can perform any mathematical computation that can be described. Another way of saying this: A universal computing machine is any machine which can execute an algorithm, or set of instructions. When you couple the ability to use algorithms (“If this, go do X. Else, do Y.”) with memory to store data, you suddenly have a machine which can take inputs and produce outputs.

i/o

Thus was the software industry born. The essential problem then became hardware – how do we make the machines which have memory and can execute programmable sets of instructions? Thus was the computer hardware industry born. The German Z3, built in 1941, was the first Turing complete digital computer. Program code was stored on punched film tape. Initial values were entered manually.

Lest we wax too historical, return to our initial question: What is a programming language? We have decided to distinguish programming languages by their virtue of being Turing complete. A programming language (or “machine”) is Turing complete if you can solve any computable problem that is describable, i.e. you can reduce it to a set of instructions. A programming language is any computer language which will let you write a set of instructions.

Conditional branching.

So what is a set of instructions? Basically, it is branching logic – “If this, then that.” You can’t (shouldn’t) really use branching logic in HTML or CSS, which is why they are not really programming languages. You just use HTML and CSS to mark up content and then style that content, respectively.

You can use branching logic in JavaScript, though. if, else if, and else are special JavaScript words that can be used to execute different code under different conditions. If this “conditional logic” sounds handy, it’s because it is. JavaScript is a Turing complete programming language.

flow chart that has "would language have let you represent the logic chain in this flow chart? --> turing complete"

Common Features Of Programming Language

Although all that they need is conditional logic, programming languages often let you do the following:

  • store a value for future use
  • loop over groupings of data
  • reuse blocks of code
  • some way to talk to the operating system
  • evaluate expressions to be either true or false (handy for that branching logic!)

In the next chapter, we will do all of these things in JavaScript.

Making Plans With Pseudocode

Before we have written any code of substance in this book so far, we have always made a plan. On a more micro-scale, you should generally make a plan whenever you’re writing any programs. To make programming plans, you should use pseudocode.

Pseudocode is a high-level, informal implementation of a piece of code. It is a description of your logic to solve a problem. It is an algorithm that has not yet been translated into programming language. Here’s an example of an algorithm for asking a user to verify that a sign-up form is correct:

function Verification(data):
  verified = DisplayVerificationBox(data)

  if verified.status == true:
    DismissVerificationBox()
    SubmitForm()

  else:
    GoBackToForm(verified.errors)

Event.Listener(FormSubmission) --> execute(Verification)

When you find yourself struggling to write your programming logic in the future, I encourage you to break out your Programming Journal and try to write out the logic in long-hand. Comments in your code are another great place to write pseudocode. Try to think top-down about whatever result you are trying to achieve – abstract your problem into smaller pieces.

Recap

JavaScript is a Turing complete programming language, which means it can solve any computable problems. Whether a problem is computable is something we’ll explore in later chapters.

Exercises

  1. Spend 5 minutes using Google.com to answer the following questions:
  • What is a program?

  • What is a programming language?

  • What is a conditional statement?

  • What is computation?

  • What is an algorithm?

  • What is computer science?

  1. Read the Introduction chapter of Eloquent JavaScript.

* Alan Turing was chemically castrated by the government for being a homosexual and committed suicide less than a decade after the conclusion of that war.

** Interpreted languages, anyway.