JavaScript Impress
JavaScript Impress
What is Data?
Data is information that we store in our
computer programs.
For example:
Your name is a piece of data, and so is your age,
the color of your hair, how many siblings you have,
where you live, whether you’re male or female…
these things are all data.
In JavaScript, there are 3 basic types of data:
●
Numbers
●
Strings
●
Booleans
Numbers are used for representing, well, numbers!
For example, you can multiply two numbers, but you can’t multiply
two strings.
With a String, you can ask for the first five characters.
With Boolean, you can check to see whether two values are both
true.
The following code example illustrates each of these possible
operations.
99 * 123;
12177
"This is a long string".slice(0, 4);
"This"
true && false;
false
Note
You may have noticed that all of these commands
end with a semicolon “;”.
Semicolons mark the end of a particular JavaScript
command or instruction (also called a statement),
sort of like the period at the end of a sentence.
Numbers And Operators
JavaScript lets you perform basic mathematical
operations like:
Addition, subtraction, multiplication, and division.
To make these calculations, we use the symbols
+, -, *, and /, which are called operators.
You can use the JavaScript console just like a
calculator.
We’ve already seen one example, adding
together 3 and 4.
Let’s try something harder...
What’s 12,345 + 56,789?
12345 + 56789;
69134
That’s not so easy to work out in your head, but JavaScript calculated it
in no time.
22 + 33 + 44;
99
JavaScript can also do subtraction . . .
1000 - 17;
983
and multiplication, using an asterisk . . .
123 * 456;
56088
and division, using a forward slash . . .
12345 / 250;
49.38
You can also combine these simple operations
to make something more complex, like this:
1234 + 57 * 3 - 31 / 4;
1397.25
Here it gets a bit tricky, because the result of this
calculation (the answer) will depend on the order
that JavaScript does each operation. In math,
the rule is that multiplication and division always
take place before addition and subtraction.
And JavaScript follows this rule as well.
1234 + 57 * 3 - 31 / 4
1234 + 171 - 31 / 4
1405 - 7.75
1397.25
The figure above shows the order JavaScript would follow. First, JavaScript
multiplies 57 * 3 and gets 171 (shown in red).
Then it divides 31 / 4 to get 7.75 (shown in blue).
Next it adds 1234 + 171 to get 1405 (shown in green).
Finally it subtracts 1405 - 7.75 to get 1397.25, which is the final result.
What if you wanted to do the addition and the subtraction
first, before doing the multiplication and division?
For example, say you have 1 brother and 3 sisters and
8 candies, and you want to split the candies equally among
your 4 siblings? (You’ve already taken your share!)
You would have to divide 8 by your number of siblings.
Here’s an attempt:
8 / 1 + 3;
11
That can’t be right! You can’t give each sibling 11 candies
when you’ve only got 8!
The problem is that JavaScript does division before addition, so it
divides 8 by 1 (which equals 8) and then adds 3 to that, giving you 11.
To fix this and make JavaScript do the addition first, we can use parentheses:
8 / (1 + 3);
2
That’s more like it! two candies to each of your
siblings.
The parentheses force JavaScript to add 1 and
3 before dividing 8 by 4.
Variables
JavaScript lets you give names to values using variables. You can think of
a variable as a box that you can fit one thing in. If you put something else
in it, the first thing goes away.
To create a new variable, use the keyword var, followed by the name of the
variable.
A keyword is a word that has special meaning in JavaScript. In this case,
when we type var, JavaScript knows that we are about to enter the name
of a new variable.
For example, here’s how you’d make a new variable called nick:
var nick;
undefined
We’ve created a new variable called nick. The
console spits out undefined in response. But
this isn’t an error! That’s just what JavaScript
does whenever a command doesn’t return a
value.
What’s a return value?
Well, for example, when you typed 12345 + 56789;
The console returned the value 69134.
Creating a variable in JavaScript doesn’t return
a value, so the interpreter prints undefined.
To give the variable a value, use the equal sign:
var age = 12;
undefined
Setting a value is called assignment (we are assigning the
value 12 to the variable age). Again, undefined is printed,
because we’re creating another new variable.
The variable age is now in our interpreter and set to the value
12.
That means that if you type age on its own, the interpreter will
show you its value:
age;
12
Cool! The value of the variable isn’t set in stone, though
(they’re called variables because they can vary), and if
you want to update it, just use = again:
age = 13;
13
This time the var keyword wasn’t used, because the variable age
already exists. You need to use var only when you want to create
a variable, not when you want to change the value of a variable.
Notice also, because we’re not creating a new variable, the value 13
is returned from the assignment and printed on the next line.
This slightly more complex example solves the candies problem
from earlier, without parentheses:
var numberOfSiblings = 1 + 3;
var numberOfCandies = 8;
numberOfCandies / numberOfSiblings;
2
First we create a variable called numberOfSiblings and assign it
the value of 1 + 3 (which JavaScript works out to be 4). Then we
create the variable numberOfCandies and assign 8 to it. Finally, we
write numberOfCandies / numberOfSiblings. Because numberOfCandies is 8
and numberOfSiblings is 4, JavaScript works out 8 / 4 and gives us 2.
Naming Variables:
Be careful with your variable names, because it’s easy to misspell them.
Even if you just get the capitalization wrong, the JavaScript interpreter
won’t know what you mean!
For example, if you accidentally used a lowercase c in numberOfCandies,
you’d get an error:
numberOfcandies / numberOfSiblings;
ReferenceError: numberOfcandies is not defined
Unfortunately, JavaScript will only do exactly what you ask
it to do. If you misspell a variable name, JavaScript has no
idea what you mean, and it will display an error message.
Another tricky thing about variable names in JavaScript is that
they can’t contain spaces, which means they can be difficult to
read. I could have named my variable numberofcandies with
no capital letters, which makes it even harder to read because
it’s not clear where the words end. Is this variable “numb erof can
dies” or “numberofcan dies”?
Without the capital letters, it’s hard to tell.
One common way to get around this is to start each word with a
capital letter as in NumberOfCandies.
For example,
if we have a variable x, then x += 5 is the same as saying
x = x + 5.
The -= operator works in the same way.
So x -= 9 would be the same as x = x - 9 (“subtract 9 from x”).
Here’s an example using both of these operators to keep
track of a score in a video game:
var score = 10;
score += 7;
17
score -= 3;
14
In this example,
we start with a score of 10 by assigning the value 10 to the
variable score.
Then we beat a monster, which increases score by 7 using the
+= operator. (score += 7 is the same as score = score + 7).
Before we beat the monster, score was 10, and 10 + 7 is 17,
so this operation sets score to 17.
After our victory over the monster, we crash into
a meteor and score is reduced by 3.
Again,
score -= 3 is the same as score = score – 3.
Because score is 17 at this point, score - 3 is
14, and that value gets reassigned to score.
Strings
Here we set the variable hadShower to true and the variable hasSuitcase to false.