Watching:

How to Use Characters and Strings in JavaScript


show more In JavaScript, characters are defined as strings contained within quotes. This can include any element that uses characters, including words and email addresses. JavaScript's flexibility makes it possible to use either single or double quotation marks, but mixing them usually creates unwanted results. In this online video from Lynda.com, you can learn the basics of using characters and strings in your JavaScript programs. show less
please wait ...

Using characters and strings

When we want to deal with text in our programs, characters, words, sentences, email addresses, we use the term strings to describe those kind of values. Now we've seen these already. Anytime you see words contained in sets of quotes, we're dealing with strings. The first line of code that we wrote was this alert message that used the string "Hello, world". This value contained in the double- quotes is referred to as a string literal, just like the number 5 or the number 1,000,000 would be a numeric literal.

We can of course use this format to create variables. I could create a new variable called message and set it equal to the words "Hello, world". I could then use that variable and pop out another alert box. So we are using the string literal to create the variable called message and then writing that variable out. Now, notice that I'm not using double- quotes around message in this second alert statement because I don't want to write out the word message. I want to write out the value of the variable called message.

Now, when we are creating strings, you can use double-quotes to surround the text or the sentence. You can also use single quotes. What you can't do is mix the two. You can't for example open with a single quote and close with a double quote. Most other programming languages restrict you to just double quotes. So I tend to use that in JavaScript to just to make it easier to go from language to language. One of the reasons you might change between the two formats is if you need to have quotes inside quotes.

Let me show you what I mean. Let's say for example we want the phrase Don't mix your quotes contained inside a string. Well, if we used single-quotes to mark out the start and the end of the string we're going to have a problem, because there's also a single quote in the middle of it, and that's how we're telling JavaScript where the string begins and where it ends. So what I could do here is use double-quotes with a single-quote contained inside them. That would be perfectly fine. However, that gets a little tougher when we have a more complex sentence.

Let's say for example we want a phrase that contains both double quotes and single quotes. Well, there is no simple way to mark out the beginning and the end of this without doing something else here. If this is what we need, what we can do is what's called escaping the quotes. This would be the way that I'd have to write it. The entire string begins and ends with double quotes and that means if I want double quotes inside that string, I mark them by putting a backslash before the double quotes being used inside the body of the string itself.

Now, one of the great benefits of dealing with strings in most programming languages is they are smart. These variables go beyond just having a holding place for some characters. We can ask things of them. We'll get into this a lot later, but just to give you a basic example, let's say I create a new variable called phrase and set it to the words "This is a simple phrase." Well, one of the things I can do is I can use the name of that variable to access or get to information about it, such as how long is it.

I can also ask questions of it, like does another word exist inside it? I can convert it to upper or lowercase. As the most straightforward example here, I am going to write the line alert and then instead of just the name phrase, I am going to say phrase.length. This is allowing me to get to what is called the length property of this variable, and what's going to happen is this will pop up an alert box that will say in this case 24. We can actually ask questions of our string variables.

As we get deeper into creating and working with any language, you will find this is very common, but variables by themselves aren't dumb. They can actually give us more information about the contents and about what they're holding. Now we'll get deeper into strings a little later on, but that's enough to get us started.

Using characters and strings
Video duration: 4m 5s 4h 47m Beginner

Viewers:

In JavaScript, characters are defined as strings contained within quotes. This can include any element that uses characters, including words and email addresses. JavaScript's flexibility makes it possible to use either single or double quotation marks, but mixing them usually creates unwanted results. In this online video from Lynda.com, you can learn the basics of using characters and strings in your JavaScript programs.

Subjects:
Developer Web
Author:
please wait ...