Start learning with our library of video tutorials taught by experts. Get started

JavaScript Essential Training

Working with strings


From:

JavaScript Essential Training

with Simon Allardice
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Video: Working with strings

So as we've seen, to create variables that are string values, you simply surround your word or your phrase with double quotes. Single quotes work as well. What you can't do is mix them up, can't have an opening single quote and a closing double quote. That can lead to issues if you need that punctuation actually inside your string. So for example, the phrase "Don't mix your quotes inside single quotes" is not going to work because the second single quote there will be perceived as closing the string. We could of course put that inside the double quotes because we're not now mixing them.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
JavaScript Essential Training
5h 31m Beginner Jul 22, 2011

Viewers: in countries Watching now:

Use JavaScript to add new features and a richer, more compelling user interface on web pages. This course keeps current best practices and practical uses for JavaScript in mind, while covering syntax, working with the DOM, and developing and debugging across multiple platforms, devices, and browsers. Author Simon Allardice also shows how to progressively enhance and gracefully degrade web pages, and take advantage of the world of JavaScript libraries now available.

Topics include:
  • Understanding the structure of JavaScript code
  • Creating variables, functions, and loops
  • Writing conditional code
  • Sending messages to the console
  • Working with different variable types and objects
  • Creating and changing DOM objects
  • Event handling
  • Working with timers
  • Debugging JavaScript
  • Building smarter forms
  • Working with CSS, HTML5, and JavaScript
  • Using regular expressions
Subjects:
Developer Web Programming Languages Web Development
Software:
JavaScript
Author:
Simon Allardice

Working with strings

So as we've seen, to create variables that are string values, you simply surround your word or your phrase with double quotes. Single quotes work as well. What you can't do is mix them up, can't have an opening single quote and a closing double quote. That can lead to issues if you need that punctuation actually inside your string. So for example, the phrase "Don't mix your quotes inside single quotes" is not going to work because the second single quote there will be perceived as closing the string. We could of course put that inside the double quotes because we're not now mixing them.

We're opening and closing the string on the double quotes alone. Problem can be if you want to have both of them inside your string. So in this case, the string itself should contain both double quotes and single quotes. If that's the case, what I have to do here is I have to escape the double quotes that I need to be used inside the string. The way that I do that is just surrounding them with a backslash and then telling JavaScript these are not intended to close the string, and they will be considered just fine within the wider opening and closing double quotes.

Now, strings can be treated as objects, and what that means is they have information we can ask of them. They have properties that we can look at. If I create a variable with this simple phrase in it then I can use the name of the variable, in this case phrase, and a dot, and I can access the length property, find out how long is the string? How many characters is the string? In this case, it's 24. That's because, in a sense, strings can be treated as arrays of characters, and the same way we have a .length property of an array, we have a .length property of a string.

Well, not only do we have properties, we also have methods. We have things that these string variables can do if we ask them. So again, creating a variable called phrase, I use that variable name phrase and a dot, and I can call methods like functions of the object. So phrase.toUpperCase, which will return, not surprisingly, the entire string all uppercased. I also have.toLowerCase. We have methods like Split. This will allow us to take a longer phrase and pull it apart.

It will actually create an array from our string. So I use the variable named phrase.split, pass it a parameter--I'll talk about that in a second--and that will return an array into the variable called words, an array of words. Now, what we're passing into the Split method is a space, just using the double quotes, space, double quotes. That means I want to split this string apart at the space, because it's not always a space. Say I have some comma-separated values.

I might call split and pass double quote, comma, double quote, create an array and split them apart on the commas. So this creates us an array that is 5 elements long, 0, 1, 2, 3, and 4, and notice that the last word, phrase, also includes the period at the end of it. But Split can be a very useful method, as can indexOf. This allows us to find out if a particular term or word appears anywhere in the string. So again, with a simple string variable, I can call indexOf, passing in the term that I'm looking for, which of course has to be another string here, and it will return the position of which that string is found, if it's found it all.

So it's going to scan through the original string, and in this case it'll say, "Oh, I find the word 'groovy' appears beginning at position 10," and it will store that result in a new variable called position. If the term that you're searching for is found right at the start, it'll return 0. Now, the question is well, what happens if that is not found at all? Sometimes that's just as important, if we need to know this term is not found. Well, what that'll do is return -1. So I could say put that in an if statement, if phrase.indexOf, pass in DDDD is equal to, again using the double equals, which will be exactly the same as triple equals here, -1, then we can do a message saying, That word does not occur.

We called indexOf. We passed in the term. It said -1. It doesn't exist there. Now, indexOf finds the first occurrence of that term in the phrase. If it could occur multiple times and we want the last one, we can also ask for .lastIndexOf. Next, the same way that we have Split to split a string apart into an array, we also have the method called Slice; to allow us to slice out a particular piece of that string. So I am going to begin with a simple phrase and I'm going to store that in a variable called Phrase.

And what I can do is now call phrase. slice, and pass in two numbers, in this case 6,11, and that is the starting position and the ending position. So what this will do is count six positions into that phrase variable, again, treating the string as an array of characters so we starting at position 0, and it will grab from position 6, up to, though not including position 11, because we are grabbing five characters, from 6 to 11. And it will return that value into the new variable called segment.

Now although it might sound like it should, slice does not change the original string. So we have a copy of those letters, but the first string remains unchanged. Now there are actually a couple of other methods in JavaScript that do very similar things, such as .substring, which in normal usage is basically identical to slice, and .substr that sounds very similar but the only difference is that instead of taking a start position and end position, it takes a start position and a length.

Just the number of characters to return. And you can use whichever one makes most sense to you. And finally, a couple of things to be aware of with string comparison. If we have two strings that are exactly the same thing, then we can use the double equals or the triple equals to match them, but our string comparison here is case sensitive, so right now if I have Hello with an uppercase H and hello with a lowercase h, they will not be considered equal to each other. If you're worried about that kind of thing, the easiest thing would be to call toLowerCase, or alternatively, toUpperCase on both of them, if you were at all unsure that there might be a casing difference in there, and then I can check them against each other.

Now something you might also be concerned about is the sort order. What happens if I use the less than or greater than? Let's say we start off with two strings: aardvark and beluga. If I want to ask, is String 1 less than String 2, this works just fine. It's going to compare beginning with the first letters, and it's going to say yes, A is less than B, so that will return true. Well how about the case where I, for example, use an uppercase B? And I'm wondering is aardvark less than Beluga with an uppercase B? Well, in fact, this case will return false.

Now it might sound a little weird, but because of the rules of sort order, all the uppercase letters are considered less than all the lowercase letters. So in this case the lowercase a of aardvark is considered to be greater than the uppercase B of Beluga. Again, something you could solve by just converting both of them into either lowercase or uppercase so that case sensitivity is not an issue. Now if you're brand new to strings, I wouldn't at all expect you to be remembering every single part of this. The important thing of any programming language is to figure out how do you remember what those methods do and what properties you have? So just do be aware that you want to have a good JavaScript reference guide, and as good as any is actually the reference guide that you'll find at developer.mozilla.org, and these are the guys that actually own the term JavaScript, so they know what they're doing here.

At this JavaScript Reference Guide what you'll find is information about things like arrays, about numbers, about strings, about some of the other stuff we'll cover later in the course. And jumping into the string information, you'll find information on getting the length of the string, individual characters, comparing strings, all the methods that you might find are useful. We couldn't really go through all of them and wouldn't want to, here. Having a reference guide bookmarked is a very useful thing with JavaScript, as with any programming language. You don't want to try and commit this all to memory.

You want to know where it is and find it when you need it.

There are currently no FAQs about JavaScript Essential Training.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked