Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In this video you're going to learn about two new types in javascript that are going to allow us to
segue into more advanced function techniques.
These two types are undefined and knowl.
Both of these represent a sort of absence of a value.
So let's go ahead and kick things off by creating a new file where we can mess around with both of these
and we'll start with undefined.
We're going to call this file undefined hyphen Nall J.
S and we're going to kick things off with an example.
We've seen countless times we make a variable let name and we assign it a value.
I'll assign this one the string Jan..
Then I print it cancel dot log.
Calling the log function passing in the name variable.
What happens when I run this.
I'm going to get gen.
We know that for a fact we've seen this example before node undefined hyphen Knol dot in J.
S and I get Jan this is not what's interesting what's more interesting and why we're having this discussion
now is what happens when we create a variable and don't assign it a value.
So let's go ahead and look at this line right here.
This line is for pieces.
We use the letter keyword we name or variable.
We use the equals sign and we give our variable a value.
But these two pieces are actually completely optional which means that this right year is perfectly
valid javascript.
We are declaring a variable name without a signing of value.
Now what's going to happen when this program runs.
Well we know that if we weren't declaring name like this the program would fail.
We would get an error trying to access a variable that's never been declared.
In this case though we have declared it we just haven't assigned it a value while to figure out what
happens.
As always the best way is to run the script.
So I'm going to run the script and what do we get.
We get the word undefined printing to the terminal.
So undefined is used in javascript to represent the absence of a value.
It's set implicitly behind the scenes.
So no point in this program have I ever typed the word undefined by creating a variable and not assigning
it a value.
Javascript has assigned it the value of undefined for us.
Now this is pretty useful because we can actually create an if statement for example and check if a
given variable has ever been assigned a value.
Right here we can set up an if statement if some condition some code to run and then we're also going
to add an else clause right on the end.
Now what's the condition we're going to see if name is undefined is the name variable equal to using
the equality operator and to actually create undefined we just type out that keyword undefined it's
a reserved keyword in the language right here.
What do we have.
We now have an if statement where we can do one thing.
If name was never given a value and another thing if it was given a value up above con. dot like right
here please provide a name and down below in the else clause.
We can just print the name because we know if this code runs that name is indeed provided.
Let's go ahead and run the script with names set to undefined.
If I rerun it I get my little message.
Please provide a name so our equality check did indeed pass.
This was true.
Now we can assign it a value either on line 1 or down below.
Name equals Jan for example.
This time around I would expect the second case to run over here.
If I do go ahead and run things that is exactly what I get I get Jeon printing because the else clause
ran name was not undefined.
Name was actually the string.
This is just one place where we're going to see undefined implicitly set for us.
So let me leave a little comment up above.
Undefined for variable.
The other place where we're going to see undefined set for us is with our function arguments are right
here undefined for function arguments and we're going to explore that with an example as well.
Right here let let's go ahead and make a function like the square function we created previously.
We're going to let it equal a function.
We'll set up a single argument numb and then we're just going to fake the interior of the function under
it going to use console dialog.
I'm going to go ahead and actually just print the Nahm value to the screen down below what we're going
to do is call Square.
Now if we were to call a square with three.
We know that three would show up.
We can run the script to prove it right here.
3 But if we weren't going to provide a value what would happen.
Well what's actually going to happen is that javascript is going to set Gnome equal to undefined.
It's not going to crash the program.
It's not going to say hey this argument is required there's no way to do that when an argument isn't
provided but it's named in the function definition.
Undefined is assigned as its value.
Now we can save the program in its new state and rerun the script and this time around you can see we're
getting undefined.
So that's the second situation we're going to see it for variables that are never assigned a value.
We're going to see it for function arguments that aren't provided and there's one more notable situation
I want to point out.
This has to do with function return values.
You know how when we return something from a function we can store its value in a variable using the
syntax.
Let's do that right now right here.
Let result equal whatever is the return value from square.
Then we will cancel dot Lague result to the screen.
So what's the return value from square.
Well we haven't used return anywhere in this function so what's the return value going to be.
Well based off of where this video has been going so far you can probably take a pretty good guess that
it's going to be undefined.
And if that was your guess you would be correct over in the terminal.
If we run this program we see we get undefined twice the first one is coming from right here and the
second one is coming from right here.
So this is the third situation where we're going to see undefined.
We're going to see undefined if we try to do something with the return value from a function but that
function doesn't actually return anything.
If I were to return something we would get that value instead.
If nothing is returned that is going to be assigned undefined.
So those are the three most common situations that we're going to see undefined implicitly set behind
the scenes.
There are others and we'll explore those as we explore more features of the language down below.
We're going to wrap up this video with just one more example.
Actually let me go ahead and leave a quick note about that last case.
Undefined as the function return default value.
Perfect.
So those are the three cases we're going to wrap this video up with just one more simple example down
below.
I'm going to create a comment but I'm not going to type anything in just yet.
And what we're going to do is create a variable age and give it a value right away.
So I'm going to define it and assign it the value of twenty seven.
Then I'm going to print it.
Cancel that log age.
We're not going to run the program.
We know it's going to happen.
We're going to see 27 print.
The question is what if in our program we want to clear this value.
Let's say the user resets the form in the browser and they want to start over.
How do we clear a variables value.
Well we can explicitly assign a variable the value of undefined and we do that by setting the variable
age equal to the value undefined.
We type out the undefined keyword and now if we were to rerun the program which I will do we will see
undefined print instead of 27.
I'm going to clear the terminal output by typing clear lips a little rendering glitch there.
And what we're going to do is rerun the script.
So let's use the up arrow key twice to get back to the script and right here we see the last is printing
undefined which lines up with this right here the problem with this code is that I'm not sure if age
is undefined because it was never given a value like in the three situations we explored above or if
it's undefined because it was explicitly cleared by the developer.
I lose that context between the actual javascript language assigning a value and US explicitly assigning
it a value and this context can be important.
So to preserve that javascript gave us access to a different type that also represents a sort of emptiness.
And this is null Knol is meant to be assigned like we're doing here so we can set age equal to instead
of undefined we said of equal to no.
Now when we run this program we're going to see Knol print instead of undefined.
I can you're on the script and that is exactly what we get.
So when we see undefined We know it's a language default when we see knowl we know something was explicitly
cleared by the developer of the program above.
We can make a note right here.
Knol as assigned value.
Excellent.
Now that we know a bit about undefined and now we're going to be able to move on to the next section
and talk about more advanced function features like passing in multiple arguments and setting default
arguments.
If the argument value isn't provided that is all coming up in the next one along with more challenges
and programming projects.
I'm very excited to get to it.
Let's jump right in.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.