Unlimited Wordpress themes, plugins, graphics & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:18Length:1.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.2 Data Types

In this lesson, you’ll learn about the available “data types” in JavaScript. Let’s begin.

2.2 Data Types

Hello, and welcome to lesson number 3 of this course were you learn about the data types in JavaScript and there are seven data types in total. Five of them are primary to our composit. Let's go ahead and start with the primary onces. These are string, number, Boolean, null, and undefined. The string is basically a group of characters. So you'll say, for example, var name = John. That's a string and you can use double quotes and you can also use single quotes, but it's usually preferred to use double quotes. The number of data type would be something like this age 32. This is written without any kind of quotes. If you would put 32 inside quotes it will then be treated as a string instead of a number. Now numbers can be either integers like this or floating points like this. 32., I don't know, 12, that's a valid number. The next type is Boolean. Now, Boolean is a special type, because It can only hold two values, very different from the other two types. It can be true or false. So I can say something like married = true or = false. The fourth primary data type is null. Now the null data type, has a single of value in JavaScript and that is null. And it's usually used to erase the contents of a variable without deleting the variable itself. All you have to do, for example, is set age to null and age will have no value whatsoever. Finally the undefined data type usually happens when you're trying to access an object property that doesn't exist or you're trying to access a variable that has not been given a value. So, for example, if I set a variable here called carModel, for example, and I go back here and I say carModel, it's gonna return undefined. While if I say age, it's gonna give me back null, so see the difference between the two. But if I say name, it's gonna return John. So those are the five primary data types. Now before we move on, I just wanna quickly show you something called typeof. Now typeof is an operator, and you do something like this typeof and it's followed by an operand. So watch what happens if I say typeof age. Typeof age gives me an object. But if I say typeof name, it's gonna give me a string. Let's try typeof carModel, it gives me undefined. So what happens is, when you are using the typeof operator, it's gonna return the type or the data type of the operand. So, for example, if I say typeof false it gives me a Boolean. If I say typeof false in double quotes, it's gonna give me a string. So it's a very nice way of checking the type of a variable. And this can be used in lots of different ways. For example, when you're doing some operations between two variables and first you want to make sure that they have the same type to prevent errors. Now that's pretty much it for the primary data types. The composite data types, I mentioned earlier, are arrays and objects. There are two of them and we'll be learning about those in the next lesson.

Back to the top