Learn JavaScript #17 | Functions - Multiple Parameters

JosephDelgadillo
58 followers
15 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 how to pass a function multiple parameters. I will also show you how to use the type of operator to identify whether something is a number, string, boolean, object, function, or undefined.
Learn more about my courses at https://josephdelgadillo.com
Alright guys, so in this video we are going to follow along with the functions section
here, and I'm going to talk about multiple values and a quick, dirty way to
have however many values you want. So, what we're going to do is first to pass
multiple variables into a function, it's literally as simple as
putting a comma and then defining the second piece of data, or the
second parameter. So, in this case, get phrase length is going to take phrase
and another. So what we're going to do is we're going to add both of these lengths
together, so I'm going to create two variables. I'll use this for one, so p1 is going to
be equal to that, and then var p2 is going to be equal to "This is a shorter
sentence." And then when we call the get phrase length we're going to pass in
both. So I'm going to save this, and now I'm going to refresh, and it still says
34 because we still only do this. So what we're going to do is phrase.length +
another.length. So we're going to save that, now we're going to refresh and
it's going to say 61. So this is the total length of both of these strings
here, and that's how to do two parameters. Now what happens if I just
pass one into that function? When we refresh it can't get the second one. So
it's going to fail because we assume that that parameter is being passed in.
Now if I remove this, save it and refresh, it works just fine, and that's because
this second parameter, as long as we don't reference it within this function,
we could do something like this. If typeof another doesn't equal
"undefined" then we can do l += another.length; and then save this.
Now we're going to pass in p2, after we put a comma in that method call here, and
I'm going to save it and refresh, now we get 61. So typeof is a way to check the
type of something. So, if I say typeof this, it's going to say "object" because
that's how we create an object, which we're going to get to in the next video.
If I say typeof this, it's going to say "object" as well, even though it's an array.
If I say typeof that, it's going to say a "number." It's going to say the same thing
for a float, and a string. So as you can see typeof...now if I say typeof vf
without putting this in quotations or anything, it's assumed that this is a
variable, as you can tell here, and we haven't defined that variable. So typeof
a variable that hasn't been created is going to return as a string
representation of "undefined." So, that's how we can check going into this method,
if typeof and then another does not equal undefined as a string. So, again,
when you're using comparisons this means not, and by using not, equals, equals it
means it is not the value, and not the type. So this is a string that represents
the word undefined. If we don't pass that in, save and refresh, it just prints out 34
and it skips over this because this type of returns undefined, so then this
doesn't even run if it's undefined. So that's a bit more
about I guess variable error checking, I guess. Rather, existence, checking the
existence of something, specifically a variable, and making sure it's not
undefined before using it. Which shouldn't really be necessary except in
this case where I'm trying to call it with only one, with only one parameter.
Now, in the next video we're going to get to objects, and I think this
is the end of the functions, at least for now.
