Function vs Method
In mathematics, a function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output. (Wikipedia)
Mathematical expression: x (input), y(permissible output)
JavaScript function for above mathematical expression
Generic way to define a function:
function nameofFunction ( ) { //body return something or //perform some action}
Let's define a simple function to understand the above definition in terms of JavaScript.
So, we can see from the above examples for each set of input like number, string, array etc. add function returned with the permissible output. For boolean input the function return NaN: Not a Number.
Now to understand difference between a method and a function is related to context. Under the hood, both suppose to perform given task or operation. If a function operating over the properties & defined within the scope of a Object; we will refer it as method. If it defined in the general context to perform operation like above example add; we will refer it as function.
Let's take the previous example
We can call add function direclty, but to call getFullName. We need Object reference of variable person.
Last updated