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

Strings As Our First Variable

Developers use variables to save and store information that they want to access and reuse in the future. Learn how to create different kinds of variables and see how they can make your life easier as your code gets more complex. In this video we will work with our first Data Type: the 'String'.

Time to explore the procedure for creating variables and using it in a program. We will first deal with strings before going to numbers. Assigning a string to a variable requires the variable to be declared first along with the data type. We declare a variable like this:

var longAssedStr: String;

Here we can see that the keyword "var" is required to declare any variable. "longAssedStr" is the variable, and the part following the colon shows what type of data this variable is. For strings, would you believe the data type is "String"?? Sure enough it is, so we type the colon followed by "String." The variable declaration must always end with a semi colon as shown.

After declaring a variable, we must assign a string to it if we want to use it later. This can be done in the same line where the variable is declared, in the next line, or elsewhere in the code. We will discuss the first two methods in this section.

Firstly, most developers prefer to declare and assign a variable in a single line: saves space and is more readable. For this we type:

var yoMamaStr: String = "wears army boots";

Here we can see that the semi colon is moved to the end of the line of code, after the string and the variable is set to equal some string within double quotes. Finally, the line must end with a semicolon. In this way the variable is declared and assigned within the same line.

There is another process where we declare the variable in one line and assign data to it in another line. For this we first declare the variable as we said earlier, along with the semicolon. In the next line we type

yoMamaStr = "wears army boots";

The result is same but two lines are used here instead of one. Remember that even the second line must be ended using a semi colon: every line of code you write must end in that semicolon! These little things are very important to remember because they can cause errors during compilation. Nasty errors.

After declaring and assigning a variable using either method, next we see how to use it in the program later. This is very simple and we just need to type the variable in the trace function to print the actual data. It looks like this:

trace(yoMamaStr);

Unlike numbers, a string is the only text data written in double quotes, so it cannot be subjected to any modifications.

Video Transcript

00:01Let's see how would we create a string value the way we create variables in flash, and I'm going to delete all these hello worlds
00:09The way we create strings in flash is by typing
00:13V AR space
00:16STR for the name of the string for now
00:19I want to call my string STR, and then we're going to then we're going to put a colon and
00:24Specify what type of variable that is although we've did we didn't learn yet about different types of variables
00:31But you could assume already because we've dealt with numbers
00:34So we could save number variables, and we could save string variables now if you're coming from earlier versions of flash
00:41Or different languages such as JavaScript that don't demand you to type what type of variable it is
00:48It's not a hundred percent mandatory to type the type of variable, but as three
00:56It's trying to be more more effective, and if you don't type the variable type
01:01It's going to be much more hard for flash to figure out what variable you actually meant for it to be
01:06So by us telling flash this variable STR is a string
01:12We're actually making it much easier for flash both to figure out what we want to do with it
01:19And also it makes our application run more smoothly because flash knows what it is
01:24So it's easier for flashing behind behind the scenes to set enough room in memory
01:29And it's easier for flash to
01:32Help us figure out if we're putting the wrong value inside of our string to output to us and output to us and tell us
01:38And help us program as well, so there's a lot of benefits of us telling our variable what type of variable it is
01:47Let's see how do we add data into this variable?
01:50I'm going to delete the semicolon if we just relook at this one more time, so we type var space
01:58str
01:59Which is the name of our variable we could have called it anything
02:03We've wanted to and then we put a semi with not a semicolon, but a colon very important to differentiate here
02:09And then we typed string
02:11so I'm going to type now
02:14equals and
02:15I'm going to open a double quote and
02:18Close a double quote and close my line with a semicolon and inside of the quote I could type my hello world
02:26So I've just created a variable which I could refer back to and the way I refer back to it
02:36I could just call str whenever I want to so if I wanted to trace our
02:41Trace out my str right now. I could type str close my bracket and
02:49it with a semi colon and
02:52Save my file and publish out and you could see that I'm getting the value of my string which is a low world
03:01Now let's make things a bit more
03:05Gradual if I didn't want to stave my data
03:09Immediately after I created it I could break the line I could create a string
03:16Break my line after I put in my semicolon and I could put into my variable later on for example in this line
03:24my value so I'm definition creating the value in one line and
03:29In the second line. I'm taking my variable that I created and I'm putting into it a string
03:35So now if I run that application one more time. We're going to see that everything works the exact same way

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!