Using characters and strings

Keyboard Shortcuts

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.

When we want to deal with text in our programs,characters, words, sentences, email addresses, we use the term strings todescribe 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 thestring "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 thewords "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 andthen writing that variable out.Now, notice that I'm not using double- quotes around message in this second alertstatement 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 thetext 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 fromlanguage to language.One of the reasons you might change between the two formats is if you need tohave 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 stringwe're going to have a problem, because there's also a single quote in themiddle 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-quotecontained 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 thiswithout 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 wantdouble quotes inside that string, I mark them by putting a backslash before thedouble quotes being used inside the body of the string itself.

Now, one of the great benefits of dealing with strings in most programminglanguages 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 thewords "This is a simple phrase."Well, one of the things I can do is I can use the name of that variable toaccess 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 andthen 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 thisvariable, and what's going to happen is this will pop up an alert box that willsay 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 thisis very common, but variables by themselves aren't dumb. They can actually giveus 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.

Resume Transcript Auto-Scroll

Author

Released

9/22/2011

This course provides the core knowledge to begin programming in any language. Simon Allardice uses JavaScript to explore the core syntax of a programming language, and shows how to write and execute your first application and understand what's going on under the hood. The course covers creating small programs to explore conditions, loops, variables, and expressions; working with different kinds of data and seeing how they affect memory; writing modular code; and how to debug, all using different approaches to constructing software applications.

Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.