How it feels to learn JavaScript
  • How it feels to learn JS
  • Table of Contents
  • Introduction
    • Is EcmaScript is JavaScript? TC39
  • JS Basics
    • Basic Concepts
    • Objects
    • Function vs Method
      • Anonymous Function
    • Scopes and keyword "this"
    • Prototypical
  • Dependencies / Packages Management
    • Chapter: Dependencies / Package Management
    • NPM
    • Bower
  • JS-Engines
    • Browser vs Node.js
  • Super / Sub - Lang
    • Chapter: Super / Sub Languages
    • TypeScript
      • Architectural Overview
  • Framework / Library
    • Angular
      • Bootstrapping
      • Change Detection
      • Singleton Services
    • React
  • Paradigms
    • Reactive
      • Rx.js
  • Web API's
    • Web Sockets
    • Web Worker
    • Service Worker
  • Testing
    • Testing Essentials
  • Patterns
    • IIFE (Immediately Invoked Function Expression)
Powered by GitBook
On this page
  1. JS Basics

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)

y = f(x)

JavaScript function for above mathematical expression

 var y = function(x) { return x; } //called as Anonymous function

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.

> function add(x,y) { return x+y; }
<-: undefined

> typeof(add) //operand typeof
<-:"function"

> add(2,3)   //number
<-: 5

> add("Hello","World")  //string
<-: "HelloWorld"  //string

> add([1,3],[7,8])  // array
<-: "1,37,8"  // string

> add(true, false)  //passing boolean
<-: NaN         //Not a Number, as + operation can't be performed over boolean.

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

var person = {
                "firstName":"John", // Property
                "lastName":"Wick", // Property
                "getFullName": function(){ // Method
                                return this.firstName + this.lastName; 
                             }
             };

We can call add function direclty, but to call getFullName. We need Object reference of variable person.

PreviousObjectsNextAnonymous Function

Last updated 6 years ago