Working with numbers
Viewers: in countries Watching now:
- Creating variables, functions, and loops
- Writing conditional code
- Sending messages to the console
- Working with different variable types and objects
- Creating and changing DOM objects
- Event handling
- Working with timers
- Building smarter forms
- Using regular expressions
Working with numbers
No special formatting is needed. It's very easy. However, there are a couple more things we need to talk about with numbers. One thing that you need to be aware of is just the behavior that happens when variables have certain values in them. So let's say we start off with a couple of variables here, var foo and var bar, both equal to the number 5. I can tell it's the number because there's no quotes around it. And we write out console.log. It's foo+bar. We will write out, not surprisingly, the number 10. If on the other hand, we say var foo = 5 and var bar = 5, these are strings.
They're surrounded in the double quotes, or the single quotes will work too. The console.log message, while it looks exactly the same, the Plus sign is going to do concatenation. It's just going to slam these two together because it thinks you want to treat them as a string. Now, sometimes you do. Say, for example, that these are holding area codes and phone numbers. You might want to concatenate them together instead of adding the numeric values together. But just be aware of that behavior. More likely you're interested in what happens in this kind of situation.
It's telling me here this expression did not make sense, and sometimes we need to know this, because think about this situation. Here's the situation we might be more concerned about. Let's say we know we're going to get a variable. Now it should be equal to a number, in this case 55, even though that's a string. But it could be abc, it could be a word, it could be some text, perhaps we're asking the user for some input. We hope they type a number, but what if they don't? Well, step one is what I try and do is I try and make a new variable, in this case myNumber, and create that by calling the function Number and passing in this variable foo.
Now again, it's the odd casing here. Is is lowercase, uppercase N, lowercase a, uppercase N. In this case, we pass in the variable myNumber and the isNaN function will return either true or false. Now, be very careful about what it's doing here. We're asking if it is not a number, so it return true if it's not a number. And we can write out the message, "It's not a number!" And what's often the case is you're actually more interested in knowing that it is a number, and we don't have isNumber function, unfortunately.
Well, I could of course just write an else statement after my if and then process some code there, but if I want to directly ask, what I'll do is phrase this as kind of a double-negative here. What I'm asking is if NOT NotaNumber is true, and I'll do that by calling isNaN function with the exclamation mark in front of it. So if isNaN returns false, we'll use the exclamationmark to flip that to true, and then we say console.log( "It IS a number" ). Yeah, you've got to love programming.
So what you'll see is a lot of Math. abilities. We have things like Math.max, which we can create multiple variables, pass in two or three or four or five, and it will just return the biggest one. Flip side of that, Math.min. And the Math object has a lot of these options. We have Math.PI, Math.random, Math.sqrt, Math.log. Useful functionality and useful behavior for working with numbers.