Learn JavaScript #18 | Functions - Flexible Parameters

JosephDelgadillo
58 followers
12 Video Views·Sep 3, 2024
Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
In this JavaScript tutorial we will cover flexible function parameters. The point of object-oriented programming is that we don't want to rewrite code, and flexible parameters allow us to perform computations on multiple bits of data.
Learn more about my courses at https://josephdelgadillo.com
Alright, so, we're going to discuss two final things about functions in
JavaScript, and that's going to lead us into talking about objects. So, with
functions we've learned how to return values, how to just process stuff, and
then also how to request, and also send through, in a function call, parameters. So,
one last thing that we need to discuss is that you can have nested
functions, and so I'm going to go ahead and create a function called printBoth,
and in here we're just going to console.log phrase and another
concatenated into one. So, console.log( phrase, another); there we go. So at this
point what's going to happen, as you guys can see, we've created the
getPhraseLength method. Let's actually change this to getPhrase, and then print
both. Now what's going to happen is, let's say I pass in p1 and p2, let's just save
this and refresh for now. It's going to say getPhraseLength is not defined
because we changed the method name. Let's name is computed because that makes
a little more sense. Then let's refresh, there. And it still does the same thing.
This is a slightly longer sentence, so it prints out the first one, let's remove
that. So what it does is it first gets the length of the first phrase, and then
if the second parameter is not undefined then it's going to add the second
parameters length to the first one. We declare a function and then we return
the length. I'm going to save this, refresh, and as you can see we get 61.
I'm not sure why break is pointed in there, or printed in
there, yeah. So, we've created a function inside a function, and we're going to use
that. So, if I were to access this like this and say printBoth, save it, it
doesn't do anything, and that's because the function there is scoped. If I try to
print it as a variable, also. Let me go ahead and return, save, refresh. That's
what I want to do. So, if you think that I would be able to access that method in
any way from outside the getPhrase function, that's what I'm demonstrating
that you cannot do. So, in this instance, what you can do is we're not going to
try and access the method when we call the method, we can't do that, but we can
access the method from within the method. So, I'm going to put a space there, and so
we're concatenate these two phrases into one. We're separating them by a space. So
what we can do is in the the getPhrase method we can then return printBoth. So
I'm going to save this now and I'm going to refresh, as you can see we've returned
from a nested function the value of this nested function to wherever we call
getPhrase. So, again, we're going to go down here and kind of see what's happening.
Let me remove some of that. Alright, so we define this method, and then let's not
worry about that right now, and then we define a parameter one and a
parameter two, and then we set computed which is a variable equal to getPhrase,
and we pass in parameter one and parameter two.
So at this point what this does is it jumps back up here, it reads this
function, says okay inside the function parameter one is going to be phrase, and
parameter two is going to be another, and then it starts processing. So we get the
length of the item, if this isn't defined we add that length, and then we declare
another function called printBoth. Now it doesn't do anything until we call it.
So here down here we return printBoth so then it jumps back up here it's like
okay, well this is actually what I should return. So, I'm going to return that here
which is going to return it here, and then we're going to console log it. So,
this does have real world applications and that's if for instance inside of
a function you need to perform some computation on multiple bits of data, you
don't want to rewrite that code. The point of programming is to write once.
I mean that's functional and object-oriented programming.
Procedural programming...procedural programming is when you don't use
functions or objects really, and that that makes it a little more complicated
because every time you want to do something you have to write it out,
wherever you are in the code. So that's why we use function. Same thing here is
just this is a slightly smaller scope. Now what happens if we don't want to pass in
that variable, but we want to pass in that variable? So what we'll do is we'll
take parameter two and we'll pass that in. In Python you can do something like this,
it's called keyword arguments, and you can type another equals, and then that. So
I'm going to save it and refresh, and let me actually return all. So we're not going
to be printing both for this we're just returning the length and we get 27.
Yeah, so, this length is 27, this length is 34, or I believed it was. So we can see that
setting another to parameter 2 doesn't, it's not doing anything, it's still
passing that in as phrase. So, what...I mean and also you might think OK, well, phrase
needs to have something, so we can set a default argument to null. So, then you
get a red underline. Now you can definitely do this in Python, and that's
part of the reason why Python is such a beautiful language, but we're not
learning Python right now. So we need to figure out how to do this, and how to
make parameters and functions more flexible, and we're going to do that by
using an object. So in the method call right here, how we create an object in
JavaScript you just open up your curly braces like that. And now you can make a
list which is kind of like an array, it's just going to have a key and a
value. So in this case we're going to set phrase: and then we're going to set
that to p1, put a comma, and then another it's going to be set to p2. There. This
right here, that's an object that has a key and a value, and then another key and
another value. So I'm going to save this, and what we'll need to do at this point
is remove both of these, and we set the incoming parameter that's
incoming to obj. Actually, no, let's set it to params, and then in here we
could access params.phrase, and then params.another. Now, if we only
wanted to get the length of another, what we have to do is we also have to change
this, params.another. Remove that method all together, that's just making things
look more complicated. So, let's go ahead and save this, and
refresh, and we get 62, and that's because what's happening now is this
method is like okay, we're going to take in one one parameter called params. So, l
is going to be equal to params.phrase.length, so this is going to be
passed in like this. The parameter one is going to become params.phrase inside
this function, and then we check if params.another is not undefined. And if that's
not undefined, then to the l variable we add params.another.length, and that's
going to add the length of this because inside the function this is going to be
referred to as params.another. So the whole object is is going to be referred
to as params, and then to access values within an object you just put dot and
then the name of the the value that you want to use. So, at that point we are
creating both. Now, what I'm going to do is I'm going to pass an empty string
into phrase, so the only value we are passing in at this point is another.
I'm going to refresh and we get 27, so that is the correct answer that we're looking
for. That's the value of that. Now, additionally, I'm going to save this and
refresh and undefined is, sorry params.phrase is undefined, so it's going
to freak out here. It doesn't know what exactly we're talking about. So we can say if
params.phrase, we're going to do this. Now the only problem is if params.phrase
does not exist then we're not creating a variable, because it creates it at this
level. So what we need to do at the very top of a function is we declare our
variable just like that. We don't assign a value to it yet, we just create it. What
we're going to do is use this variable like that. So if params.phrase then
this is going to become that, and then if you know another is not
defined then we're going to add that. So I'm going to save this and refresh, and we
get not a number. That's because that technically does exist, let me do this. If
typeof params.phrase doesn't equal defined, save it, refresh,
still get not a number. That's because we didn't set a number. So it wasn't set to
become a number and it didn't become a number here, so when this tries to to add
length to this it wasn't a number, so it couldn't do that. So, that's a little bit
about objects, and we're going to be jumping more into objects in the next
video.
