Complete Intro to Web Development, v3 Numbers, Strings & Booleans
Transcript from the "Numbers, Strings & Booleans" Lesson
[00:00:43] A string of characters. So, a string of characters is just words or letters or numbers or literally anything here. So I can put whatever that can be numbers, it can be that kind of stuff, I think I can even put emojis here, if I want to, right? It's totally fine with all of those things.
[00:01:02] So basically, any sort of text you would imagine, totally fine, you can put that all here as long as you have your quotes. Even an empty string where there's nothing in between it, totally okay. Even whitespace, right? You can see down here, it's just logging out all this whitespace.
[00:01:23] So there's that double quotes is okay, you can also do single quotes. Sometimes those are useful if I need double quotes inside of my string, Right? Because when you put this single quote, it's only gonna end it when there's another single quote. Now, you might ask here, why are there backslashes here?
[00:01:52] It's because it has to escape them. So notice here, it has double quote here and a double quote here to denote the full string. How does it know that the string is not ending here? Well, it has to escape it, right? So for example, here I have single quotes up here.
[00:02:14] If I put, I don't do that, it thinks it ends here, right, because it hits another single quote. So how do I prevent that? If I put a backslash, then it's okay with that. Now, you might think like, well, I don't want that backslash to show up in my code.
[00:02:31] It's totally fine, it will not. It's just this particular software that does it. So for example, if I say, we're just going back here. We'll put semicolons, don't do that. If I save that and reevaluate that here in my experiments, Totally fine, nothing's amiss, right? The next thing people ask me is, how do I do a backslash then have a backslash escape something, right?
[00:03:05] If I do that, but not here rather. You just put a double backslash, which will end up being a single backslash. Okay, the other one I'll call out really quickly is I showed you single quotes, I showed you double quotes. You can also use what are called backticks.
[00:03:25] So you know on your tilde key, which is right next to your 1 key on your keyboard. You can also do, Backticks like this. This is also valid as well. Which ends up being really nice because, how many of you have ever used a backtick for literally anything else?
[00:03:50] I actually frankly don't know what it's for beyond coding. Someone's gonna tell me in the comments, I'm sure, but I actually don't notice for I've never really thought about that before. So it's really nice for strings because you never have to use backticks for literally anything else. The other nice thing about backticks is you can do multiple lines of it.
[00:04:17] Whereas with single quotes and double quotes, it doesn't work, but with backticks, it does. So, that is a string, right? It's just an arbitrary collection of characters, Okay? So let's get on to a bit more complicated example here, I have const firstName, Brian lastName, Holt. Now, what happens if I wanna put the strings together?
[00:05:11] Cuz if I don't have a space there, it's literally gonna shove them together because you didn't tell it to put a space there, right? So you actually have to put that literal space there, or it'll try and smash those things together. So same thing here, I do Hello + firstName +, I have to put a space there, or else this space right here won't exist, right?
[00:05:32] If I just get rid of that entire little thing right there, it's gonna shove Brian Holt straight together. So it gets a little burdensome because you have to do a lot of concatenation to get the exact string that you want. So I wanted to show you how to do this with the backticks.
[00:05:48] So, notice these are double quotes, notice these are backticks again, that same one that I showed you on your tilde. You can do this special syntax here with the dollar sign in the curly braces to just put variables inside of your strings. And that makes this. Yeah, I need a plus sign here.
[00:06:14] So sentence are 4 and 5. Obviously, they log out the exact same thing. In your opinion, which one do you think's more readable? I think the one on line five looks more readable to me. So again, this is called a template string, by the way, what I'm showing you.
[00:06:40] This is brand new with, again, as of 2015. You have to use backticks so these templates won't work in single quotes right? So if I do this, it's literally going to put that string into my string, right? So one, you must be using backticks, and then use dollar sign and then curly brace.
[00:07:07] Which if you've ever done bash programming, that's how you do it in bash as well, fun fact. If you don't know what bash is, there's a whole course on introduction to Linux in the CLI, I'm told very attractive, gentlemen. It's me, I did it. Okay, so that is strings and string concatenation and putting strings together.
[00:07:36] Strings are very useful, obviously, you use strings everywhere because you need. So yeah, again, when I just say string, I'm talking about words, characters, this kind of things. Things that go between quotes, that's what a string is. And that's common amongst all programming languages. Let's talk about booleans.
[00:07:55] Booleans are just true or false. So if I come in here and say, let isBrianCool = true, now I have this, isBrianCool, and you can see that is true. I mean, it says it in my browser, it's undenied, everything that is on the Internet is true. So therefore, Brian is cool because it's on the Internet now, okay?
[00:08:22] And then by the same token, you can say, let isSkyGreen = false, right? And then you have this, Other variable isSkyGreen, and it is equivalent to false. So that is called boolean, it can either be true or it can be false, and that's it. It's a boolean variable.
[00:08:46] I think boolean is actually the name of some mathematician that came up with the concept of these binary things, and so they named it after the mathematician, I think. And by the same token, you can also have numbers, right? So let x =, or let's say, let age =, I don't know, 25 or something like that, right?
[00:10:09] Sometimes you think it should be and it's off by 0.0000001, right, which makes it not equal, but you'd expect it to be.