Loading
Current section: 1. Expressions And Output 7 exercises
solution

Escaping Strings

Transcript

00:00 Let's get started by using console dot log. It's working. Now my AI assistant was like, it'll be easier to just use double quotes here, but I wanna show you how to escape with a single quote. So we have single quotes. We escape with this backslash right here.

00:17 The backslash is just a special character inside of a string. It's a special syntax that says, the thing that follows this character is not to be interpreted as usual. So normally, that would be interpreted as the end of the string. We are saying don't interpret it as that. It is this special apostrophe character.

00:36 So, my, formatter is actually going to replace that with double quotes. And the formatter is just that. It's just formatting. It's not gonna change behavior of the program at all. It's just changing it to be a little bit easier to read because you can actually format this like this.

00:54 And, that is a perfectly valid program, but it's hard to read. So the formatter is just gonna make things look a little bit nicer. And one of those things is replacing the double quote with or the single quote with a double quote so we don't have to escape. So that there there you go. That's kind of interesting.

01:11 But now you know. And here, we want to do double quotes, and we're going to escape that. So that is how, you use double quotes and escape those double quotes. But again, it's the formatter is gonna say, how about we just do single quotes for this one? Now here's one where the formatter is not going to change anything, because it would change behavior if it tried.

01:32 And that is to use double quotes. You could use single quotes as well. But we want the words hello and world to be on separate lines. So we're gonna say hello and then we use a backslash and the letter n. So normally, if you just did n, it would be hello hello hello hello on world.

01:53 But, in our case, we actually want a new line there. And so we're going to add this backslash that will interpret that n as a special character being a new line character. And so if I save that, you're actually not gonna see that in here. And, actually, by the time you take this workshop, I'll I'll get that fixed, so that this logs properly. So I'm gonna pull this up just to make sure that we've got this working.

02:15 And here we get hello and then new line character and world. So that is working. And then finally, tabs are another thing that, that you can do. So here we can, add a bunch of, like, table headers or whatever. We've got name, age, city, John, 25, New York, Jane, 30, Los Angeles, and Jim, 35.

02:35 Thank you AI for helping us make that, work nicely. You can't really see that in here either. I'll see if I can fix that, by the time you take this, workshop. But if you run the, node index, you'll see that those tab characters are working as they you would expect, and those things are lining up nicely like it's a table. So there are, like I said, a ton of different things that you can escape in JavaScript strings.

03:03 These are just a couple of the most common ones. There are actually better ways to do multiline strings now in JavaScript as well with back ticks. And we'll look at that a little bit later, pretty soon. But this, hopefully, gives you a good idea when you're reviewing your AI code or whatever what's going on inside of those strings.