prep

Prerequisites: a terminal, basic arithmetic.

Evaluating expressions

Learning Objectives

💡Tip

Computers work by storing and performing operations on data.

Computer programs are built from many expressions. We must understand how expressions are evaluated to understand how computer programs are executed.

We can take an expression like 36 * 45 and ask what it evaluates to. If we know what the * operator represents (multiplication) and if we understand the arithmetic rules represented by the operation we can evaluate this expression ourselves.

Happily, computers can evaluate expressions for us.

NodeJS is an application that runs JavaScript programs. In other words, NodeJS can understand and execute programs written in JavaScript. One feature of Node is the REPL.

📝Note

REPL is a special type of program that stands for:

  • Read - Users enter some code that Node will read
  • Evaluate - Node will then evaluate this code
  • Print - Node will print the result to the terminal
  • Loop - Node will loop back to the beginning and prompt users to input some more code

With a REPL we can run pieces of code and look at what happens.

We input JavaScript instructions that are then executed by NodeJS. The REPL replies with, or prints out, the result of this execution.

Type each of the following expressions into the REPL one at a time and then press enter to check the result.

10 + 32
32 / 10

🕹️Activity

In this activity, you’ll check you’re ready to use the Node REPL on your machine.

  1. Open the terminal on your computer
  2. Check you’ve got Node installed on your computer
  3. Start the Node REPL in your terminal
  4. Enter the expressions and evaluate them using the Node REPL

If you don’t know how to do any of the steps above, then try searching for an appropriate command online. Searching for things when you’re stuck is super important part of being a developer!

🕹️Activity

Create your own expressions and enter them into the Node REPL.

🧠 Before you type in the expressions, predict what the REPL output will be. Write your prediction down and compare it to the outcome.