Vous êtes sur la page 1sur 2

Working with numbers

So anytime we're working with variables, we really have two things. We have the variable, the container,
the bucket, and we have the different values that we're going to put in it from moment to moment. Now,
the most common kind of values in computer programs are numeric values, numbers, and really this is no
surprise. Sure, these days, when we use the word computer, we think of a machine. But for hundreds of
years, computer was a job. In the 17th, 18th, 19th centuries, you could be employed as a computer.
It just meant a person who computes, someone who does calculations. Then these machines come along
and they do it far better than the person ever could. These days we think a computer means
hardware. But the reason they were invented was always about number crunching. But we need to be
comfortable with our use of numbers. When we're writing programs, we're dealing with bank balances and
dollars and cents, the position of a spaceship on the screen, the height and width of a web browser
window, the coordinates of a device using geolocation, the amount of milliseconds we've been playing an
MP3 song, even colors are represented with numeric ranges of red, green, and blue. All numbers.

Now, unlike many programming languages you'll come across, where you need to be very conscious of
what type of number you have-- integer, floating-point, positive, negative, is it a big number, is it a small
number-- JavaScript instead has a very human approach to numbers. They're all just considered
numbers. So let's see how to work with them. So I have an empty file here set up to write some
JavaScript in. Let's start off this way, var a. Now, this statement tells JavaScript create a variable and call
it a. When this line of code is executed, the value of this variable is undefined.
So on the next statement, I will set that variable to the number 5. Now, when you're brand-new to
programming, lines like a = 5 and b = 10 can look a little weird, particularly if the last time you saw
anything like this was equations in school. But remember, when you see the equals sign here, this is not
a politedescription. This is a command. This is an instruction. This is saying take the value 5 and put it in
the variable called a.
I don't care what a was before, but after this line of code is executed, it will have the value 5. This is what
the equals sign does. It assigns a value. That's why it's formally called the assignment operator. Now,
notice that there are no quotes around the number 5. The number 5 here is referred to as a numeric
literal. It represents the fixed value 5. The variable part of this line can change as our program runs, but
the literal does not.

5 always means 5. What if we want to change this? What if we want a million? Well, I'll just say a =
1000000. We don't use commas in it. We just use the number itself. While in other programming
languages we have to be very conscious if values are integers, meaning whole numbers, JavaScript
doesn't care. If we want something after the decimal point, we just type that in. Numbers are regarded as
positive by default, but they can be negative as well.
If I want to make a negative number, I just put a minus sign in front of it. In this case -500. So we have
five statements so far. I'm going to add a sixth. I'm going to use alert again, and in parentheses, just put a.
Now, notice that I'm not using quotes here. I don't want to write out the letter a as an alert dialog box. I
want to write out the value of the variable a, which is what this is going to do. So I'm going to save this
and I already have a folder set up where I can just run this JavaScript by opening the HTML page.
So I open that up and we get one dialog box with -500 in it. That's what we should expect. What's
happening is we're opening up, our program runs, our code runs, we create the variable, we change it to
5, we change it to 1000000, we change it to 123.654, we change it to -500, and then we write it
out.Simple instructions created and executed one after the other. But this is how we can start to work with
number variables in JavaScript.

Now, as one final note, understand there's a very big difference betweencreating a variable like this, var b
= 123, and creating a variable like this, var c =. Similar looking value, but inside quotes. Well, the first one
here, var b, is a number 123. The second one, var c, is a string. Not the number 123, but the digits 1, 2, 3
and sometimes that's a pretty big difference in behavior.
So let's talk about strings next.

Vous aimerez peut-être aussi