Locked lesson.
About this lesson
Understanding the difference between strings and numbers; and understanding Boolean data types.
Exercise files
Download this lesson’s related exercise files.
Strings, Numbers, and Boolean Data Types.docx58.7 KB Strings, Numbers, and Boolean Data Types - Solution.docx
58.9 KB
Quick reference
Strings, Numbers, and Boolean Data Types
Strings, numbers, and booleans are all different types of data.
When to use
Strings, numbers, and booleans are something that you'll use all the time.
Instructions
What type of data a thing is, is important.
When trying to add a string to a number, JavaScript will put the two together into a string.
For example: 4 + "John Elder" becomes 4John Elder
Booleans are either True or False.
Hints & tips
- If you try to add strings, JavaScript pushes the two together.
- If you try to add a number to a string, JavaScript pushes the two together.
- Booleans are either True or False.
- 00:05 In this video, I wanna talk about strings, numbers, and Boolean data types.
- 00:09 So, we've already played around with strings.
- 00:11 That's just var myName = John Elder.
- 00:17 That's a string.
- 00:18 And we've already looked at numbers var myNum = 41.
- 00:24 I'll talk about them a little bit more in this video, but
- 00:27 I also wanna talk about Boolean data types.
- 00:29 And that maybe something that you haven't heard before.
- 00:31 So strings, numbers and Booleans are all types of data.
- 00:34 These are all data types, right?
- 00:36 And knowing what kind of data type they are and
- 00:38 what you can do with that specific data type is important.
- 00:41 For instance, you can add two numbers, but you can't add two strings, right?
- 00:46 We've already sort of talked about this.
- 00:47 You also can't add a string to a number, but let's go ahead and try.
- 00:51 So if we wanna to come back here and do document.write and
- 00:58 then add myName + myNum.
- 01:02 So what do we think is gonna happen here?
- 01:04 You can't, what's 41 plus John Elder?
- 01:07 So if we come back here, it smushes these together, and
- 01:10 it just turns them into a string, right?
- 01:12 So that's kind of interesting.
- 01:13 So what happens if we try to add two strings?
- 01:17 So let's go, Is Awesome.
- 01:20 So if we save this,
- 01:20 what is John Elder + Is Awesome, likewise it just smushes these two together.
- 01:25 And you could see there's no space in between them or anything.
- 01:28 And one little trick you can do if you wanna put these things together,
- 01:31 you can a space to one of them and it kinda does that.
- 01:33 But that's just sort of a hacky little thing.
- 01:35 It doesn't have anything to do with what we're doing here.
- 01:37 Likewise, earlier, remember we had myNum 41.
- 01:43 If we change this string to 2.
- 01:48 If we save this, this is not a number.
- 01:50 Remember I mentioned earlier it's got quotation marks around it, so
- 01:53 it's still a string, but it looks like a number.
- 01:55 So what happens here, same thing It just smushes the two together,
- 01:58 turns it into a string.
- 02:00 So that's kinda fun, but what exactly is a Boolean?
- 02:02 A Boolean and that's just in case you're wondering, B-o-o-l-e-a-n.
- 02:09 A Boolean just means true or false.
- 02:11 We often use Boolean when we compare things we've just talked about comparison
- 02:15 operators and we were actually using Boolean back then.
- 02:18 I just didn't really call them that 'cause we were trying to decide is two equal to two?
- 02:22 Yes, it is that's true.
- 02:24 True is Boolean.
- 02:25 Not only do we use it to sort of determine if a thing is true or
- 02:28 false but we can also make a variable into a Boolean.
- 02:32 So we can go var myVariable = true, right?
- 02:37 So now the variable, myVariable is true likewise we can call it false.
- 02:43 You'll learn more about Boolean later on when we start doing if statements and
- 02:46 loops and things like that.
- 02:47 But often it's useful to test if the thing is true or not and
- 02:50 we will do that when we will use Booleans.
- 02:52 So that's strings, that's numbers, that's Booleans,
- 02:55 those are data types we will look at some of them later on in the course.
- 02:58 So, in the next video we will look at arrays.
Lesson notes are only available for subscribers.