02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Numbers As Our Second Variable

Developers use number variables to save and store information (numbers) that they want to access and reuse in the future. Learn how to create different kinds of number variables and see how they can make your life easier as your code gets more complex.

We played around with math and played around with Strings: isn't it time to throw around a few variables that can store numbers? We think so!

We discussed strings as variables earlier. We all know how to declare a string variable and how to assign data to that variable. The method of declaring and assigning a Number variable is the same. The only thing is we use a different keyword here during declaration: 10 guesses what! If you said "Number," you win the booby prize. This will ensure that the variable is of type Integer (Number) and can take data that is in the form of digit(s) that are of type Number.

When it comes to assigning data to the variable, we can assign only what it takes. So data assigned in form of digits is easily understood by the application and it compiles easily when needed. But when we assign data of type string to this variable, the application causes an error (pukes, remember?) at the time of compilation. The application cannot recognize a string for that variable because it was declared to be Number. Therefore, the compiler wants the data contained by the variable to be of type Number. Any data type other than integer (Number) is considered as wrong input from the user and therefore causes an error. While dealing with numbers, another area that needs to be looked into is the correct syntax. We see that the syntax for dealing with variables is the same for both strings and numbers, but it isn't exactly the same. When it comes to a Number, there is a small change: no single or double quotes here while they are compulsory for a String.

Here is an example of declaring a Number variable:

var numNutts: i

Now you have an integer variable all ready to store your numbers! Easy as that.

Video Transcript

00:00So now that we know how to create strings, let's see how to create a number.
00:04We're not going too much into depth into what strings are.
00:08We're going to look what numbers are, and then we're going to start playing with
00:12strings and numbers and seeing the differences between them.
00:16So I'm going to create one more variable here. I'm going to create a new variable,
00:20and I'm going to call it num for number, and I'm going to again
00:24put a colon, and I'm going to define it as a number.
00:29And notice how I always start with an uppercase letter.
00:32So string is uppercase S, and number is with an uppercase N.
00:38And now I have a new number that was created, which I could then
00:43go to my num, equals, and place inside of it whatever number I want.
00:49So let's put inside of our number the value of, let's say, 50.
00:55Now we want to publish this with, we want to trace our num, so I'm going to
01:01get rid of the word str, and I'm going to trace out num.
01:06And if I save the file and publish, we're going to see that the value 50
01:10is outputted, and there aren't any errors.
01:13But if I would take my number, which is a number value,
01:17and I would, for example, send into it hello instead,
01:22and publish the file, it would generate an error.
01:27As it generates the error, it's going to give us a notice telling us
01:30in the description that in line 9, which is the line that we're in,
01:35there was an implicit correction of a value of type string
01:38to an unrelated type number.
01:42Basically telling us that Flash was expecting from us to send a number
01:46into this value, but instead of it, we sent a string.
01:49So we don't want to send a string, we want to send a number.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!