Check out a free preview of the full Complete Intro to Web Development, v3 course

The "Numbers, Strings & Booleans" Lesson is part of the full, Complete Intro to Web Development, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Brian discusses the different data types in JavaScript and demonstrates defining numbers, strings, and booleans. Utilizing backticks, string concatenation, and the difference between whole numbers and floats are also covered in this segment.

Preview
Close

Transcript from the "Numbers, Strings & Booleans" Lesson

[00:00:00]
>> So let's talk about numbers and strings and booleans. So JavaScript has a lot of these different kind of data types. You don't really have to worry too much about data types then, JavaScript is what's called the dynamic language. That means it's pretty fast and loose. If we were in C++ or Java, this would be a lot more picky about what we did about this.

[00:00:20]
If that means nothing to you, that's fine. That's probably actually good. [LAUGH] But let's talk about all the different kinds of things that you can use in JavaScript. So for example, here I have const myName = "Brian Holt". Now, these double quotes are really important cuz this denotes to JavaScript, I'm gonna give you a string, right?

[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:04:51]
This is called string concatenation. So sentence = Hello, and then I can just put a plus sign here and JavaScript isn't smart enough to say, you're putting two strings together, I can do that. So, this first sentence says, Hello, and then notice that there's this space here. Why do I have a space there?

[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:09:16]
I mean, we were already using that up here or in the previous one when we're talking about rents and stuff like that. JavaScript just has one type of number, lots of other languages will separate integers from what are called floating points or decimals, right? JavaScript doesn't do that, JavaScript just has the one type called numbers.

[00:09:35]
So if I say, let, I don't know, babyWeight = 10.5 or something like that, right? It doesn't differentiate between this being an integer and this being a decimal. It actually makes it kind of hard sometimes because it makes JavaScript a little imprecise, right? You don't really have to worry about that today, but sometimes asking is like, is this number equivalent to this number?

[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.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now