BTG Coding School

Basic expressions

Welcome back! With our first programs written, lets get back to basics and learn what makes up a Javascript program.

A program is just a very specific set of instructions for the computer. Just as there are many spoken languages for communicating with humans, Javascript is just one of many languages for communicating with computers.

A javascript language is mostly build up of two types of things. “Statements” tell the computer to do something, and “expressions” are a way of representing some value that you would like the computer to calculate.

Statements end with a semicolon. In the last lesson, we wrote two statements that the computer executed:

console.log("hello world!");
alert("hi there :)");

Normally, statements are the only things that you can see immediately. When using devtools, you can also see the value for any expression you enter.

Expressions are ways of expressing some value that the computer can calculate. The simplest form of value is a number, which mostly behaves like you’re used to in mathematics.

Try these one at a time:

1
1 + 5
20 * 3
5 / 0
5 ^ 2
5 ** 2
6 * 20 + 3
6 * (20 + 3)
"my favorite number is " + 10

When presented with an expression, Javascript determines what it needs to evaluate first, then works up, replacing pieces as it goes. Any value on its own just evaluates to itself. For math, Javascript follows the standard order of operations. However, if there is any likelihood of being confused of what the computer will do, programmers often add in parentheses to be clear. Programs are written both for the computer to run, but also for other programmers to read and understand.

Built-in operators

The things that we put between values are called operators. These operators are primarily involved with math, but you’ll see a few others later.

One surprising thing you may have noticed is that 5^2 is not 25. The ^ operator is actually called “exclusive or”. If you want to calculated 5 squared, you want to use ** for power.

The last one there is a bit of a Javascript superpower. We saw double quotes in our first lesson, which is a way of representing text. Though I’ll go into this in more detail in the next lesson, I just want to point our that you can actually add text to numbers. In this case, the number is converted to text, then the two are joined together.

Function expressions

You may be wondering how to write expressions that require more than two inputs. Our operators know that they use the things on either side of them, but when writing programs this limits us to two inputs to anything.

For expressions that have more than two inputs, or for things that dont have a nice symbol to represent them, we can use “functions”. We actually used functions already. Both console.log and alert are functions. We write a expression that uses a function by putting the name of the function, parentheses, then all of its inputs separated by commas.

Here are two pays of writing the power expression we saw earlier:

5**2
Math.pow(5, 2)

More information on functions is coming up later :)

Feel free to experiment with what you’ve learned so far. Programming is best learned by applying what you’ve learned. A common beginner mistake is to read or watch a lot of material without practicing. Without practice, it is hard to be able to recall your knowledge when you go to write programs.

Exercises

Here are a few math expressions. Can you convert these into Javascript expressions in order to get the right value?

  1. $$ 1 + 2 + 3 \over 2 $$
  2. $$ \sqrt{1522756} $$ (use Math.sqrt)
  3. $$ |-19| + {35 \over 5} $$ (use Math.abs)

Answers

  1. (1+2+3)/2
    
    gives us 3
  2. Math.sqrt(1522756)
    
    gives us 1234
  3. Math.abs(-19) + 35/5
    
    gives us 26

Next lesson: Types

Up next we’ll be moving beyond numbers and looking at some other types of values we can use in our programs.