2.1 String Basics - Video Tutorials & Practice Problems
Video duration:
3m
Play a video:
<v Instructor>We'll start off with some quick string basics</v> which we'll investigate in the node repo. Like this. We saw this in the last chapter. We saw a hello world, like this. (keys clanging) So this thing in double quotes is a string. It's a string of characters. But we can also just type this out. I can copy and paste here. We can just type this out as a regular string. So now we're not printing it out. You'll see that it actually has a return value which is just itself. It's evaluated to itself. Like that. Now you might have noticed here that I've put in the string with double quotes like this, and then the node repo printed out the return value using single quotes. This is the kind of thing that is system dependent. It depends on what you're going to use for your repo. I think it might be different in the console. In fact, we could even check. Let's do a quick check. (keys clanging) If you look at the console here, I can paste this in actually. So I selected that. Actually, well, let's do, let's do it, let's do with single quotes. Look at that. So as you can see here that it actually outputs here. This says the web inspector console, it output it with double quotes. So I put it in with single quotes and now with double quotes and I can also just to do this here. Hello world. That was jump cut, by the way, really cool little utility for maintaining multiple strings in the buffer. So double quotes, also double quotes. Right. Let's close this down. Go back here. So this is actually something that's unusual to JavaScript. Most languages have some distinction between double quotes and single quotes, but in fact, in JavaScript, they're exactly the same, except for just sort of irrelevant details having to do with escaping out of characters. So let me show you what that means to escape something out. So suppose we were to write something like it's not easy being green, and type it out like this, 'it' apostrophe 's', (keys clanging) and you can see here, the repo prints it out with a back slash in front of this apostrophe and that's to distinguish it from the closing quote. So if I were just to type this out without the back slash, we can go up arrow here, replace this. Control a gets us to the front of the line. So this will be an error, syntax error: unexpected identifier. So what's happening here is that JavaScript via the node evaluation engine says, ah, opening quote, 'it', closing quote, and then suddenly what is this 's', I have no idea what this 's' is. It actually thinks this 's' might be a variable identifier. We'll be talking about that later. So in order to get this apostrophe to work if we're using single quotes we have to escape it out with a back slash, like that. But other than that, it's pretty much identical. The double quotes and single quotes are the same. And generally speaking, I'll switch back and forth between them without any particular explanation. I'll generally err, on the side of using double quotes but if a single quotes creep in, well they're equivalent. There's one especially important string that will come up from time to time, which is an empty string also known as "the empty string". So this is like this two double quotes. And of course you can do this as well. We'll be talking more about the empty string later in this chapter, and again, in the next chapter.