Transcript of the tutorial.
TOP
All right, then.
So hopefully now you're going to be quite clued up as to what a function is.
It's a block of code to do something that can be invoked and run at any point in our code.
So now I want you to cast your mind back to when we were talking about strings and numbers and other
data types.
Now we use something called methods on those data types, which did something for us.
And I said that methods and functions are kind of synonymous with one another because they do the same
thing, right?
They can be invoked to run a bit of code, which does something for us.
So a method is just a function.
Now what distinguishes methods is the way that we invoke them and where they are defined.
Now if we have just a regular function or an arrow function, and in fact I'm going to create this,
I'll say const greet is equal to an arrow function and this arrow function is just going to return a
value.
Hello.
Now what I'm going to do is I'm going to store a result, we'll call it result one equal to greet.
So now when we invoke this function like this, we're getting this value back and we're storing it in
this variable.
So we could log this to the console.
We'll just log result one and this will work.
So we should see.
Hello.
So notice when we invoke this regular arrow function, we just invoke it like this, the function name
and then parentheses.
Now when we use a method, the method is invoked using dot notation.
So remember we could take a string and use a method on it called to uppercase.
And the way we do that is by taking that string and saying dot to uppercase.
So the value then dot notation, then the method name.
Now this is still just a function, right?
But the way we invoke it is different.
We're using dot notation right here on the value itself, whereas here we're not doing that.
We're just invoking it directly by saying the function name and then parentheses.
In both cases, we do still use parentheses and in both cases we can pass in arguments to them.
The only difference in how we're calling them is that we're calling a method on the back of something
using dot notation.
So what I'm going to do is store this in a result as well.
So let result to equal to this thing and then I'm going to console dot log result two and this is going
to work.
So I'll save it and preview and we can see this right here.
Sean So then methods are functions, but they're functions which are associated with an object or data
type if you like, like a string.
They're defined on an object or data type, whereas our function was just defined here on its own,
not on an object or data type.
So we've not covered objects yet, so I don't want you to freak out thinking you don't know what they
are, but they're one of the seven data types in JavaScript.
In the future we will look at objects and then create our own methods on our own objects.
And we'll also be using many methods built into the JavaScript language like this thing right here as
well.
But for now I just wanted to plant the seed to let you know that these methods that we use are still
just functions that do something for us.
They're essentially the same thing just with a different name.
And the difference is how we call them and where they are defined.
So that is good enough for now.