Primitive vs. Complex variables
Its time for a summery but more then that its time for us to see and understand the difference between primitive data types and complex data types in JavaScript.
Video Transcript
00:00Alright, so in our source files, you'll find the object.js file, and I'm just going to use one of our index files.
00:06And I'm just going to import it in currently. It's empty, but if you open it up in the source files, you'll find it to build up with whatever we're working on right now.
00:15So let's get started. I want to start off first with a quick summary of what we know so far.
00:20We've learned about basic variables, which we now can give them a name because we've learned about two different types.
00:27We've learned the primitive types, which we said were numbers, and booleans, and strings.
00:32So just for this case, let's just put here a number, we'll call it num, we'll say it equals 10.
00:37And let's create one of those more complex variables.
00:40So I'm going to create quickly a complex variable, and I'm going to call it date because that was the first complex variable we ever created.
00:47And we said that when we create a complex variable, we always start with the word new, space, and then the name of the class, which we don't really know what a class is.
00:56But basically, it's a blueprint that enables us to create objects of different sorts.
01:01And this time around, we want to create a new date, which is an object which has a lot of stuff that are related to dates.
01:08And I know that there's a lot of words here that if you're not familiar with programming really well, some of them could become a bit confusing.
01:15So I would say don't worry about the namings too much. Just realize that idea that there's, in our case, we call it a class,
01:23but there's this source that enables us to create new elements of a certain type.
01:29And we already learned about all the different components of those elements, if it's variables or functions.
01:35Basically, we're saying create a new bucket that contains variables and functions, and that's all we're really doing.
01:41The names that we call them is really just a programming way of saying it.
01:45We have classes that create objects, so this end is the class end.
01:49We're creating a new class of the type of object. We're taking that class, turning it into an object.
01:56All right. Now, the date type is not the most basic one, and we're actually going to get into the basic one, which is basically an object.
02:05I didn't start with object, although most places where you'll learn programming will start with the object object,
02:11because the object object is empty.
02:14And if we use with object object, there's a couple of functions there and a couple of properties that don't have a lot of usage for us.
02:19And we would have to create our own public, we would have to create our own functions and variables.
02:25And in this case, in scenario, because we're using something that's more advanced, a date object,
02:29that date object already contains a lot of stuff that we could just start playing with it.
02:33We're not creating our own unique functions and signatures within our object that we created.
02:39But before we jump into seeing what is a basic object and how to create things,
02:43I just want to point out a very important differentiation between those two things that we've learned so far.
02:49Contrary to primitive variables, where I could create a new variable num2 and say it equals num,
02:57and then go to the original num and then change its value,
03:01if I would then try to output the value of num2, the value of num2 is not going to equal 5.
03:08That is because, although num equals 10,
03:12as soon as I set the num value into num2, I passed 10 into num2.
03:19Now when I change my variable, it doesn't have any effect on the original variable.
03:24And that's a very important thing to note, and this is only true with primitive variables.
03:29So if I save this and I run our code here, let me just find our button,
03:35live code, and you can see that we're tracing out here a 10.
03:41We can see that live view is all we needed, we don't have to have live code as well,
03:45but basically we get the value 10, which is exactly what we expected to get.
03:49Contrary to that, if we were trying to check what's in num right now,
03:53we would see that num did get updated and num got that 5.
03:57Now, this is absolutely true, but only true to primitive variables.
04:03Now, if I go to my date object and I create another date,
04:06and say I call it date2, and I set inside of date2 the value that's inside of date.
04:12Okay, now if I make any changes to the date object itself,
04:17it's going to affect my date2 as well.
04:21So let's see this in action.
04:23So first of all, I'm going to go into the original date,
04:25and I'm going to search for something that I want to change.
04:27So currently if we do a quick alert for our date,
04:30and we'll see that our date is a specific date and time.
04:33Right now when I'm doing this recording, it's Monday, April 30th, 2012.
04:38Let's change the year, let's say, to 2011.
04:41So to do that again, I'm going to go into my date object,
04:44and I'm going to click on set year,
04:47and you know, let's go a little bit more crazy.
04:49It's 1982 now, so I'm setting the year,
04:52and if we rerun it one more time,
04:54we're going to see that now we're on April 30th,
04:56this time around it's a Friday, and it's 1982.
05:00Now if I go to date2, which is a completely different variable,
05:04if I go to date2 and I try to check what the value is in date2,
05:08we didn't change date2, we changed date, right?
05:11But if I do a refresh on this, we're going to see that it changed as well.
05:14And that's really one of the most critical things you have to understand,
05:17which a lot of developers don't.
05:19That's if you don't go through the formal training,
05:21and you don't find, this is like one of the most critical stuff
05:23that you need to get really used to really master variables.
05:28And that's really the key component here.
05:30There's a difference between primitive variables,
05:32which are basically the content itself,
05:34so when I save it into another variable, I'm transferring the data,
05:38I'm not transferring a pointer.
05:40Contrary to that, objects are a collection of a lot of data.
05:44When we say that a variable equals a specific object,
05:47it doesn't really equal that specific object,
05:50it equals a pointer to that object that was created.
05:53So basically, by creating another variable such as we did in this state,
05:58we're just putting another pointer to the same position.
06:01So basically we're saying, let's give a new name to the same variable.
06:05It does not mean let's create a duplicate, okay?
06:09And that's something that a lot of people get confused at.
06:13Primitive values, you're duplicating.
06:17Complex variables, which are types of objects,
06:21you are not duplicating, you're only referencing to the same thing.
06:25So it doesn't really matter if I have 10 different variables here
06:28that are called the same thing.
06:29So that's why just one more thing to lock this up
06:32before we move on to continuing into object is,
06:35this is also true when you're passing variables into a function.
06:39If you're passing in a primitive value,
06:41it will never affect the original value that was sent.
06:45You could do changes inside of your function.
06:47Contrary to that, if you send an object,
06:49and that function modifies that object,
06:52that function is modifying the original object.
06:55And that's something that's really important to notice.
06:57A lot of the types of bugs and things that you don't really expect
07:00come from this specific issue.
07:02It's a very hidden and almost impossible thing to notice
07:06when you have a really big application.
07:08So it takes a lot of work to find out and figure it out.
07:11So you just have to make sure that any time you're doing something,
07:14and what you're doing on this level,
07:16if you're editing the original,
07:18or you're editing a duplicate.
07:20And that's all I have to say about this.
07:22Hopefully this was a big one,
07:24because if you didn't know this yet,
07:27I'm happy you watched this,
07:28because this could change your life as programmers.
07:31I'm being a bit dramatic, but it's true.
07:33Without this piece of the puzzle,
07:36it would be really, really hard to understand
07:38why things stop working, and they would stop working.
