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

Strings

We will often be working with strings, so let’s take a look at some common manipulations we can do to them using JavaScript.

Naming Conventions

Every variable has a unique name which differentiates it from other variables. One important thing to remember about JavaScript is that it’s a CASE SENSITIVE language. In other words if you create a variable named string1 and you want to use this variable anywhere in your JavaScript, you can't refer to it as 'String1': JavaScript will puke! (The difference is only the capital ‘S’.) If you forget this tidbit of information, you're begging for trouble.

Note: If we are talking about variable names, it's important to note that in JavaScript and many other programming languages the standard is to camel case variables: always start with a lower case letter. In our case we would prefer 'string1' and not 'String1' as our variable name. More on why we do that later.

Since we’re talking about variables, here’s some sound advice. As you’ll be using a lot of them in your programming, we suggest you always create variables which have a useful name. If you want to create a variable to hold the first name of a person, don’t name it it something random like ‘var cow;’. Use names that describe the variable's content such as ‘var firstName’. The more descriptive your variable names are, the more readable your code will be.

In most programming languages, when you finish writing one line of code you end it with a semi colon “;”. If you don’t, an error might be displayed. JavaScript is a forgiving language: if you don't use the semi colon you might not get an error (you don’t want to rely on this particular MIGHT!) It's good programming practice so I’m going to encourage you to use the semi colon whenever you end a line of code.

Using Variables with Strings

Getting back to creating variables in JavaScript, let's take a deeper look into the creation of a variable:

var firstString = “Hello Earth”;

We just created a string which is stored in the variable firstString. Now, what is a function? A function is a piece of code which performs a task. For example JavaScript has a function called alert(“ ”). This function's job is to take a string input and display it in the client's browser as a pop-up message. If we pass this function the string which we just created like this: alert(firstString); when the page loads, a pop-up message will appear saying “Hello Earth.”

JavaScript offers a lot of ways to manipulate strings. Imagine you want to combine two strings together. All you need is a PLUS(+) sign. For example:

var firstString = “Hello Earth” + “, I am back”;

What happened here? The + sign joined the two strings together and stored it in the variable firstString: now firstString has “Hello Planet, I am back” stored in it.

The same can be done with variables. For example if we have var testString = “Hello People” what happens when we call alert like this:

alert(testString + testString);

You got it right if you guessed we get a pop up box showing “Hello People Hello People.” String variables can also be joined using the magical + sign.

What’s the point of this lecture and all these tips? As you gain experience as a programmer you will realize how important it is to have clean code with meaningful variables. It not only helps you but it helps anyone who reads it to manage and understand your code.

Video Transcript

00:00So I'm going to create a variable inside of my script tag,
00:03VAR space, and we give that variable a name.
00:06So we're going to create our first variable
00:08is going to be the hello world first.
00:09So it's going to be our, I like starting with S first copy.
00:15And I want you to notice that S is for string,
00:18so I'll know that this is a type of string.
00:20First copy, I'm uppercasing my F, uppercasing my copy.
00:25And that is just so it's easier for me to read.
00:27So when I'm looking at this, I could see S first copy.
00:29I know that this is my first copy.
00:32It was really easy to read because I uppercased all of it.
00:34Now there are a lot of different techniques.
00:36I say pick whatever technique you want, and then stick with it.
00:40And in the beginning, maybe use my technique
00:41until you get more comfortable, and then you
00:43can decide whatever technique you want.
00:45Now, some programming languages will now
00:48at this point, such as Flash, ActionScript
00:51would then define what type of information
00:53that variable contains.
00:54JavaScript, though, currently doesn't.
00:56So JavaScript, you just define a variable,
00:59and that variable now exists.
01:01And now I could put inside of it whatever type of data I want.
01:03I could put a string inside.
01:05I could put a Boolean.
01:05I could put a number.
01:08In our case, we want to put a string inside.
01:10And to be able to put a string inside for JavaScript
01:14for the browser to know that we're
01:16trying to put a string inside, the way we do it
01:18is using or a quotation mark in the opening of the copy,
01:25and then closing it with a quotation mark.
01:29Or we use a double quotation.
01:31There's absolutely no difference, as long
01:32as we start with a double quotation
01:34and end with a double quotation, or start with a quotation
01:38and end with a quotation.
01:40Now, it's a tradition in programming languages
01:42in general, although you probably
01:43won't get an error in some.
01:45Some programming languages have no idea when a line finishes.
01:49So although we just finished, and when we put an enter,
01:51and now we're a new line start,
01:52and want to do something else, applications
01:55could get confused.
01:56Because sometimes we'll want to break a line just
01:58to make things look better.
02:00And sometimes we're breaking a line
02:01because we really want to complete that line.
02:04Now, some languages are more forgiving.
02:06Some languages are less forgiving.
02:08JavaScript is a more forgiving language.
02:10So if we don't tell it that I'm done with that line,
02:13we might not get an error.
02:15Then there are other languages, such as PHP,
02:17that if we don't explicitly say that we finished the line,
02:20it's going to generate an error.
02:21Now, most programming languages, with the way
02:24you define the end of a line, is by putting a semicolon.
02:28You put a semicolon in the end of the line,
02:30that semicolon tells that your browser, in our case,
02:35that we've completed this line, and we're starting a new one.
02:38And by the way, if I wanted to,
02:39I didn't have to break a line.
02:40I could start my next variable in the same line
02:42because from the programming perspective,
02:45I just finished a line of thought.
02:47And I could start a new line of thought.
02:49It could be on the same line.
02:50But obviously, for reasons of keeping things
02:53more convenient and more visually comfortable,
02:58we'll probably break a line each time
03:00when we complete that semicolon,
03:02basically completing the line.
03:04Now, we created now our first variable,
03:08and I want to already start talking about that functions,
03:12that scary words.
03:13Of course, if you haven't heard about functions,
03:15functions are basically actions.
03:18Well, a variable stores information,
03:20a function does something.
03:22A function does something, a variable stores something.
03:26So in our scenario, we have a variable.
03:28It's called S first copy.
03:29It stores information.
03:31What information does S first copy store?
03:34It stores the string, hello world.
03:37Now, I want to call a function that already exists.
03:39It's called alert.
03:40And we've seen that alert a second ago.
03:43Previously, we sent to it an explicit line of copy,
03:46saying, Ben is saying, hello.
03:50And we close that bracket.
03:53This basically is the syntax for calling functions
03:55or actions, you could call that for now.
03:57What action are we calling?
03:58We're calling the alert action.
04:00What are we sending that alert action?
04:02We're telling it to use the Ben is saying hello,
04:05which is a string, like we've learned a minute ago.
04:08And then we end that line by using that semicolon.
04:11So if I save this file again and I go to my application,
04:13we'll see that Ben is saying hello in our alert box.
04:18Now, what I really want to do
04:19is I want to use this new variable that I've created.
04:22Very easy, all we have to do is copy that variable
04:25and we can go inside of our function
04:27and basically delete all that words
04:29that were in that action and paste your S first copy.
04:32We could save that information.
04:33Now, I'll ask you guys already in advance, guys and girls,
04:36if I run my application now, what will I get outputted?
04:39Will I get outputted S first copy?
04:42Or will I get outputted hello world?
04:45Gonna give you a second to think.
04:47I think he had a good answer, hopefully.
04:49And he said to me right now, hello world.
04:51And if you said hello world, you were totally right.
04:53And that's exactly what would come back.
04:55So basically our variable is really, really useful.
04:58It enables us to store information, then use it later on.
05:02In our scenario, we created it in line 12
05:04and used it in 14.
05:06But what if we wanted to use hello world now
05:08a couple of times?
05:09So we could have had your S first copy
05:11plus S first copy.
05:13What would that do?
05:14It would combine the first line of this variable,
05:17hello world with a low world again,
05:19creating a hello world, hello world
05:21without a space here, right?
05:22So if I refresh my screen one more time,
05:25we're gonna see hello world,
05:26which was our first time our variable.
05:28And the second one starting immediately after hello world.
05:31All right, so we're seeing our variable
05:32and we're seeing it in action.
05:34And we see how we could use plus
05:36to connect strings together.
05:38And this is just a side note.
05:39Different programming languages
05:41use different connecting languages.
05:43So in JavaScript, we would use plus
05:45to connect hello world, said Ben.
05:51All right, so I'm connecting this string with this string.
05:56So together, what would happen
05:57if I'm gonna delete this here one second?
05:59So by connecting these two together,
06:01now the S first copy now equals
06:04hello world, comma, said Ben.
06:08Now, if I wanted to make our lives
06:09a little bit more complex,
06:10I could then go to S first copy.
06:14And it's really, really important
06:15if you called it S first copy
06:17to name it exactly the same way.
06:18No lower cases.
06:20JavaScript is a case sensitive language.
06:22Well, let's find out actually.
06:23Okay, so we're gonna try this variable
06:27without caring about this case sensitivity.
06:30And if we refresh again,
06:33we're gonna see that it's not working.
06:35So JavaScript is case sensitive.
06:37Now, if we look here on the side,
06:39we'll see that we got here some sort of error.
06:41And if I double click on it,
06:42and I'm gonna open up here on the screen,
06:44we'll see that S first copy is not defined
06:48with the lower copy.
06:49Basically saying that, in other words,
06:52we cannot send an alert message, nothing.
06:54We have to send a certain amount of copy.
06:56And in our scenario,
06:57S copy with a lower case
06:59is a completely different variable
07:00than S first copy exactly in the way that I created it.
07:05All right, so, and by the way O2,
07:07JavaScript is a really forgiving language in other ways.
07:10So if I have your S second copy,
07:14and I say here, I say,
07:20and I try to combine here the two variables.
07:26So notice how this is,
07:27it's starting to look complicated, but it's not.
07:29So I created a first variable here.
07:31I didn't really create this variable, not explicitly.
07:34And then I'm calling it my functions.
07:36Let's see what happens if I do that.
07:37Will I get any errors?
07:38No errors, it's working.
07:40Now, although JavaScript is very forgiving,
07:42and you're allowed to do these variables
07:44without defining them,
07:45I really encourage you to always use a definition
07:48because it helps you stay more structured,
07:51and it makes sure that you know
07:52where that variable was defined.
07:54And that is really, really important.
07:56And you're gonna appreciate it as you mature
08:01as a developer, and as you use JavaScript more and more,
08:03it's really important for you
08:04to know the scope of your variable.
08:06And it's too early for us to delve into what scope is,
08:10but if you know what a scope is
08:11from another language that you worked on,
08:13you'll understand the importance.
08:15So always define your variable.
08:17And later on in our learning,
08:19hopefully we'll get to a point
08:20where we could explain what scope is
08:22and why it's so critical for you
08:24to always save your variable and define it
08:27and know where you're defining it.
08:30So we've seen here how we connect strings together,
08:33and we've also seen how we could create,
08:34connect two variables together.
08:38Now, in our scenario,
08:40and ignore this line that looks complicated
08:42because we don't really know what functions are.
08:45You could do it,
08:46and I actually obviously encourage you to do it.
08:49But for now, let's say that this is,
08:52we're not worried too much about this.
08:54What I wanna do is I wanna create a third variable.
09:00Let's just call it S3, make our lives easier.
09:04So S3, and now again,
09:06I'm gonna take my S first copy, upper case, S second.
09:16So basically what I'm doing,
09:17I'm combining the first copy with the second copy,
09:19putting it into S3.
09:21And now I'm just gonna output that out,
09:23S3 with the alert message, save my file again, refresh.
09:27We're gonna see that copied the same way as we expected it.
09:31Now, another thing I want you to notice,
09:33and obviously we're not gonna be able to get
09:34into every single in and out of the language in this 101,
09:37but we'll definitely have more trainings on this,
09:39is that in our scenario,
09:41if we now use a variable that doesn't exist,
09:43if I put here a lowercase copy and I saved it,
09:46JavaScript is not gonna generate an error in this case,
09:49or it will.
09:50And you can see that JavaScript is generating an error,
09:53but it's not as easy to notice.
09:55And if we go into our error messages,
09:57and if I just clear my old errors
09:59and we refresh it one more time and go there,
10:01we'll see that S first copy is not defined in line 14,
10:04which is actually a very, very descriptive error.
10:07And we could get back here line 14,
10:10and then we could find that we had our issue there.
10:12Fantastic.
10:13So our Firefox browser is giving us
10:15a really good error management.
10:17Every browser has a different level of error management,
10:20and sometimes it's really hard to debug.
10:23That's why I was trying,
10:24my point was really, really notice
10:26that you're putting your variables in the right way.
10:28In the beginning, it might be hard,
10:30but the more you do it and the more you're consistent
10:32with your naming,
10:34the easier it is and the less common
10:36you're gonna have issues of this type.

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!