Variables and Expressions
We should begin by talking about values and data types. Data types are the different kinds of data we can use and store. We have: Numbers: 9 Strings: 'Computer' Boolean Values: True/False We can also talk about operators. A good start for operators is to talk about mathmatical operators, such as addition (+) and multiplication (*) In lively, we can evaluate any block of text by highlighting it and pressing CMD/CTRL + D and print the result by highlighting it and pressing CMD/CTRL + P. Try that below on the following VALUES and OPERATORS:
Values and Data Types
var a = 0; var b = 3; alert(a+b);
This time, try highlighting all 3 lines of code and pressing CMD/CTRL+D Notice how a box pops up giving you the result of the operation.
3+4
3*4
3+4*5
(3+4)*5
'hello' + 'world'
Notice that the strings 'hello', ' ' and 'world!' can be added together, but that the following doesn't work:
'team' - 'ea'
Variables
Next, let's talk about variables. How do you get a computer program to keep track of a value for future use? To do this, you assign them to what we call 'variables'. A variable is assigned a name for future referencing, like this:
Expressions
A fragment of code comprised of any number of values, operators and functions that produces a value is called an expression. In fact, an expression can be composed of other expressions, much the way sentences are composed of words and fragments. On the previous page, the lines we evaluated were expressions, like this one:
3+4
Expressions can be combined to form larger and more complex expressions, much like complex sentences can be built from subsentences and fragments. Below is a series of expressions combining to form a larger expression:
var a = 3; var b = 4; var c = a + b; var d = a - b; c * d;
If you highlight all of those lines and press CMD/CTRL+P, they are all evaluated and the result of c*d is printed out. This is the same as saying this:
(3+4)*(3-4)
but using expressions to build larger and more complicated ones allows for simpler constructions:
var a = 3; var b = 2; var c = a+b; var d = a-b; c*d+c/d
is much simpler than writing:
(3+2)*(3-2)+(3+2)/(3-2)
And much easier to change a single thing later, if you need to. For instance, what if I suddenly want all the 3s to be 5s?
Operators
Operators, as we discussed before, perform actions on values. Previously, we saw what are referred to as Binary Operators, because they perform an action on two values, such as addition or multiplication. Not all binary operators produce another value of the same type, however. Below you will find a few expressions that evaluate instead to what are referred to as 'boolean' values; that is a value that can only be either true or false.
3 > 2;
3 < 2;
If you highlight each line in turn and evaluate it, you will receive either a true or false result. These are examples of boolean expressions. We will use these expressions quite a bit in the weeks to come.
In addition to binary operators, we also have what we call unary operators. These operators act on a single value instead of a pair. Highlight both lines below and press CMD/CTRL+P
var a = 3; typeof a;
typeof is a unary operator and, in this case, returns the type of value stored in a. We can also use the "++" operator to increase an integer by 1, as follows:
var a = 'hello world!'; typeof a;
Unary Operators
Logical Operators
There are also operators which can be applied directly to pairs of boolean values called logical operators. There are 3 logical operators: AND, which is represented by && IF, which is represented by || NOT, which is represented by !
Each of the following statements will evaluate to true:
true && true;
true || false;
!false;
var a = 1; a++; alert(a)
This is mostly used in loops, which we will discuss at a later time, but it's useful to know what they are now.
Logical operators will see extensive use later