Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
00:00.210 --> 00:04.530
In this video you're going to learn about two new types in javascript that are going to allow us to
00:04.530 --> 00:07.450
segue into more advanced function techniques.
00:07.530 --> 00:11.390
These two types are undefined and knowl.
00:11.400 --> 00:14.660
Both of these represent a sort of absence of a value.
00:14.760 --> 00:18.980
So let's go ahead and kick things off by creating a new file where we can mess around with both of these
00:19.140 --> 00:21.060
and we'll start with undefined.
00:21.060 --> 00:25.260
We're going to call this file undefined hyphen Nall J.
00:25.260 --> 00:28.260
S and we're going to kick things off with an example.
00:28.260 --> 00:33.410
We've seen countless times we make a variable let name and we assign it a value.
00:33.420 --> 00:36.280
I'll assign this one the string Jan..
00:36.390 --> 00:39.310
Then I print it cancel dot log.
00:39.390 --> 00:44.050
Calling the log function passing in the name variable.
00:44.100 --> 00:45.240
What happens when I run this.
00:45.240 --> 00:46.060
I'm going to get gen.
00:46.080 --> 00:53.730
We know that for a fact we've seen this example before node undefined hyphen Knol dot in J.
00:53.730 --> 01:00.390
S and I get Jan this is not what's interesting what's more interesting and why we're having this discussion
01:00.390 --> 01:06.950
now is what happens when we create a variable and don't assign it a value.
01:07.140 --> 01:10.200
So let's go ahead and look at this line right here.
01:10.200 --> 01:11.390
This line is for pieces.
01:11.400 --> 01:14.170
We use the letter keyword we name or variable.
01:14.310 --> 01:17.250
We use the equals sign and we give our variable a value.
01:17.460 --> 01:23.340
But these two pieces are actually completely optional which means that this right year is perfectly
01:23.370 --> 01:25.080
valid javascript.
01:25.080 --> 01:30.540
We are declaring a variable name without a signing of value.
01:30.540 --> 01:33.020
Now what's going to happen when this program runs.
01:33.120 --> 01:38.250
Well we know that if we weren't declaring name like this the program would fail.
01:38.310 --> 01:42.250
We would get an error trying to access a variable that's never been declared.
01:42.390 --> 01:47.190
In this case though we have declared it we just haven't assigned it a value while to figure out what
01:47.190 --> 01:47.940
happens.
01:47.970 --> 01:50.650
As always the best way is to run the script.
01:50.670 --> 01:53.310
So I'm going to run the script and what do we get.
01:53.310 --> 01:57.530
We get the word undefined printing to the terminal.
01:57.720 --> 02:03.540
So undefined is used in javascript to represent the absence of a value.
02:03.610 --> 02:06.270
It's set implicitly behind the scenes.
02:06.270 --> 02:12.900
So no point in this program have I ever typed the word undefined by creating a variable and not assigning
02:12.900 --> 02:13.920
it a value.
02:13.920 --> 02:18.600
Javascript has assigned it the value of undefined for us.
02:18.600 --> 02:23.220
Now this is pretty useful because we can actually create an if statement for example and check if a
02:23.220 --> 02:26.310
given variable has ever been assigned a value.
02:26.310 --> 02:33.030
Right here we can set up an if statement if some condition some code to run and then we're also going
02:33.030 --> 02:35.760
to add an else clause right on the end.
02:35.760 --> 02:43.110
Now what's the condition we're going to see if name is undefined is the name variable equal to using
02:43.110 --> 02:49.860
the equality operator and to actually create undefined we just type out that keyword undefined it's
02:49.860 --> 02:53.030
a reserved keyword in the language right here.
02:53.100 --> 02:53.790
What do we have.
02:53.790 --> 02:56.730
We now have an if statement where we can do one thing.
02:56.730 --> 03:04.890
If name was never given a value and another thing if it was given a value up above con. dot like right
03:04.890 --> 03:10.770
here please provide a name and down below in the else clause.
03:10.770 --> 03:16.730
We can just print the name because we know if this code runs that name is indeed provided.
03:16.920 --> 03:19.830
Let's go ahead and run the script with names set to undefined.
03:19.890 --> 03:22.960
If I rerun it I get my little message.
03:22.960 --> 03:26.710
Please provide a name so our equality check did indeed pass.
03:26.710 --> 03:28.170
This was true.
03:28.180 --> 03:32.770
Now we can assign it a value either on line 1 or down below.
03:32.770 --> 03:35.410
Name equals Jan for example.
03:35.410 --> 03:39.340
This time around I would expect the second case to run over here.
03:39.340 --> 03:46.330
If I do go ahead and run things that is exactly what I get I get Jeon printing because the else clause
03:46.420 --> 03:49.130
ran name was not undefined.
03:49.150 --> 03:51.380
Name was actually the string.
03:52.240 --> 03:57.240
This is just one place where we're going to see undefined implicitly set for us.
03:57.250 --> 03:59.580
So let me leave a little comment up above.
03:59.870 --> 04:03.550
Undefined for variable.
04:03.580 --> 04:08.740
The other place where we're going to see undefined set for us is with our function arguments are right
04:08.740 --> 04:17.000
here undefined for function arguments and we're going to explore that with an example as well.
04:17.020 --> 04:23.440
Right here let let's go ahead and make a function like the square function we created previously.
04:23.440 --> 04:25.170
We're going to let it equal a function.
04:25.360 --> 04:30.760
We'll set up a single argument numb and then we're just going to fake the interior of the function under
04:30.770 --> 04:32.990
it going to use console dialog.
04:33.340 --> 04:38.620
I'm going to go ahead and actually just print the Nahm value to the screen down below what we're going
04:38.620 --> 04:40.690
to do is call Square.
04:40.720 --> 04:42.640
Now if we were to call a square with three.
04:42.640 --> 04:44.310
We know that three would show up.
04:44.320 --> 04:46.940
We can run the script to prove it right here.
04:47.090 --> 04:52.370
3 But if we weren't going to provide a value what would happen.
04:52.540 --> 04:58.870
Well what's actually going to happen is that javascript is going to set Gnome equal to undefined.
04:58.900 --> 05:00.630
It's not going to crash the program.
05:00.730 --> 05:06.250
It's not going to say hey this argument is required there's no way to do that when an argument isn't
05:06.250 --> 05:09.460
provided but it's named in the function definition.
05:09.730 --> 05:12.210
Undefined is assigned as its value.
05:12.310 --> 05:18.250
Now we can save the program in its new state and rerun the script and this time around you can see we're
05:18.250 --> 05:19.410
getting undefined.
05:19.570 --> 05:24.480
So that's the second situation we're going to see it for variables that are never assigned a value.
05:24.640 --> 05:29.950
We're going to see it for function arguments that aren't provided and there's one more notable situation
05:30.010 --> 05:31.230
I want to point out.
05:31.270 --> 05:34.090
This has to do with function return values.
05:34.300 --> 05:39.080
You know how when we return something from a function we can store its value in a variable using the
05:39.080 --> 05:39.900
syntax.
05:40.000 --> 05:42.500
Let's do that right now right here.
05:42.940 --> 05:48.720
Let result equal whatever is the return value from square.
05:48.850 --> 05:53.900
Then we will cancel dot Lague result to the screen.
05:53.920 --> 05:56.320
So what's the return value from square.
05:56.320 --> 06:01.660
Well we haven't used return anywhere in this function so what's the return value going to be.
06:01.780 --> 06:06.790
Well based off of where this video has been going so far you can probably take a pretty good guess that
06:06.790 --> 06:08.160
it's going to be undefined.
06:08.170 --> 06:12.640
And if that was your guess you would be correct over in the terminal.
06:12.790 --> 06:18.910
If we run this program we see we get undefined twice the first one is coming from right here and the
06:18.910 --> 06:21.190
second one is coming from right here.
06:21.280 --> 06:24.700
So this is the third situation where we're going to see undefined.
06:24.820 --> 06:31.330
We're going to see undefined if we try to do something with the return value from a function but that
06:31.330 --> 06:33.890
function doesn't actually return anything.
06:34.030 --> 06:37.390
If I were to return something we would get that value instead.
06:37.390 --> 06:41.590
If nothing is returned that is going to be assigned undefined.
06:41.800 --> 06:46.570
So those are the three most common situations that we're going to see undefined implicitly set behind
06:46.570 --> 06:47.440
the scenes.
06:47.470 --> 06:53.270
There are others and we'll explore those as we explore more features of the language down below.
06:53.290 --> 06:56.760
We're going to wrap up this video with just one more example.
06:56.770 --> 07:00.280
Actually let me go ahead and leave a quick note about that last case.
07:00.640 --> 07:07.870
Undefined as the function return default value.
07:07.870 --> 07:08.600
Perfect.
07:08.650 --> 07:13.990
So those are the three cases we're going to wrap this video up with just one more simple example down
07:13.990 --> 07:14.670
below.
07:14.890 --> 07:17.750
I'm going to create a comment but I'm not going to type anything in just yet.
07:17.890 --> 07:22.690
And what we're going to do is create a variable age and give it a value right away.
07:22.690 --> 07:26.340
So I'm going to define it and assign it the value of twenty seven.
07:26.470 --> 07:27.380
Then I'm going to print it.
07:27.610 --> 07:29.640
Cancel that log age.
07:29.680 --> 07:30.850
We're not going to run the program.
07:30.850 --> 07:31.940
We know it's going to happen.
07:31.940 --> 07:34.060
We're going to see 27 print.
07:34.060 --> 07:38.240
The question is what if in our program we want to clear this value.
07:38.350 --> 07:42.840
Let's say the user resets the form in the browser and they want to start over.
07:42.850 --> 07:45.930
How do we clear a variables value.
07:46.060 --> 07:53.530
Well we can explicitly assign a variable the value of undefined and we do that by setting the variable
07:53.530 --> 07:56.740
age equal to the value undefined.
07:56.740 --> 08:02.620
We type out the undefined keyword and now if we were to rerun the program which I will do we will see
08:02.650 --> 08:06.010
undefined print instead of 27.
08:06.310 --> 08:12.410
I'm going to clear the terminal output by typing clear lips a little rendering glitch there.
08:12.520 --> 08:14.950
And what we're going to do is rerun the script.
08:14.950 --> 08:22.390
So let's use the up arrow key twice to get back to the script and right here we see the last is printing
08:22.420 --> 08:27.940
undefined which lines up with this right here the problem with this code is that I'm not sure if age
08:27.940 --> 08:34.330
is undefined because it was never given a value like in the three situations we explored above or if
08:34.330 --> 08:38.050
it's undefined because it was explicitly cleared by the developer.
08:38.050 --> 08:45.280
I lose that context between the actual javascript language assigning a value and US explicitly assigning
08:45.280 --> 08:47.740
it a value and this context can be important.
08:47.860 --> 08:54.140
So to preserve that javascript gave us access to a different type that also represents a sort of emptiness.
08:54.160 --> 09:02.440
And this is null Knol is meant to be assigned like we're doing here so we can set age equal to instead
09:02.440 --> 09:07.010
of undefined we said of equal to no.
09:07.020 --> 09:11.120
Now when we run this program we're going to see Knol print instead of undefined.
09:11.220 --> 09:14.110
I can you're on the script and that is exactly what we get.
09:14.130 --> 09:20.520
So when we see undefined We know it's a language default when we see knowl we know something was explicitly
09:20.520 --> 09:24.230
cleared by the developer of the program above.
09:24.300 --> 09:27.140
We can make a note right here.
09:27.280 --> 09:30.670
Knol as assigned value.
09:30.750 --> 09:31.850
Excellent.
09:31.920 --> 09:36.480
Now that we know a bit about undefined and now we're going to be able to move on to the next section
09:36.690 --> 09:42.360
and talk about more advanced function features like passing in multiple arguments and setting default
09:42.390 --> 09:43.380
arguments.
09:43.380 --> 09:49.230
If the argument value isn't provided that is all coming up in the next one along with more challenges
09:49.260 --> 09:50.820
and programming projects.
09:50.820 --> 09:52.420
I'm very excited to get to it.
09:52.530 --> 09:54.100
Let's jump right in.
13002
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.