amklogo

JavaScript Difference between Methods and Functions

Home » JavaScript Difference between Methods and Functions

JavaScript Difference between Methods and Functions

JavaScript Functions: A function is a block of code written to perform some specific set of tasks. We can define a function using the function keyword, followed by Name and optional parameters. The body of a function is enclosed in Curly braces.

Syntax: 

function helloName(parameters) {
     // Just Write Hello World In The Console
    console.log("Hello World!");
}

Features:

  • The function is executed when something calls/invokes it.
  • Arguments are values a function receives when it is invoked.
  • Parameters are listed inside round parenthesis after the name of the function.
  • The name of the function may contain letters, digits, dollar signs, underscore.
  • When the control reaches the return statement, JavaScript will stop executing and the value is returned to the caller.

Example: Below is the function to print your name in the console.

function helloWorld(parameters) {
    const name = "Amran";
    console.log(`Hello ${name}!`);
}

Output:

Hello Amran!

JavaScript Methods: A JavaScript method is a property of an object that contains a function definition. Methods are functions stored as object properties. Object method can be accessed with the following syntax:

Syntax:

object = {
    methodName: function() {
        // Some Code...
    }
};

object.methodName()

Features:

  • Actions that can be performed on objects are what we term JavaScript methods.
  • The objects can also be called without using parenthesis.
  • This refers to the owner object in a method.

Example: The following example shows the method that is invoked with an object called employee.

const employee = {
  name: "Amran",
  department: "sales",
  details: function() {
    return this.name + " works with Department " + this.department;
  }
};
console.log(employee.details());

Output:

Amran works with Department sales

Difference Between Function and Method:

Function:
  • A function can be called directly by its name.
  • A function can pass the data that is operated and may return the data. 
  • Data passed to a function is explicit.
  • A function lives on its own.
Method:
  • A method consists of a code that can be called by the name of its object and its method name using dot notation or square bracket notation..
  • The method operates the data contained in a Class.
  • A method implicitly passes the object on which it was called.
  • A method is a function associated with an object property

you can find more useful articles on AMK

Share:

I’m Front End Developer with more than 2 years of experience, I love to learn every day, and share this knowledge with other developers. if you are reading this now, I wish you have a great day!

28 Comments on JavaScript Difference between Methods and Functions

    Badt0men
    October 18, 2021

    Nice and simple... 7 views... I came all the way from dev Community

      Amran Al Kerata
      October 19, 2021

      Thanks bro!🌹

    Moshe Klienblat
    October 19, 2021

    Nice and short

    Steven
    October 19, 2021

    Useful and short article.

    HopeTech-IT Solution
    November 10, 2021

    Hi, Mahmud Hasan here from HopeTech-IT Solution. We are a professional IT solution service provider based in Bangladesh (South Asia). First of all, I apologize for making such a comment on your website. If you think this comment is not necessary, please delete it. No such comment will be made on your website again.
    .
    Recently your website caught my attention while searching for brand new websites. After entering the website, it seems that your website is brand new or not yet properly configured. That means you may need a professional help. And if that happens then, I think I'm in the right place. As a IT solution service provider, We have dedicated professional team members who can provide all kind of web-related services like Web Design Development (WordPress), Graphics Design, Search Engine Optimization (SEO), Virtual Assistant services at competitive prices.
    .
    You can visit our official website to know the details and price of our services. Feel free to contact us if you are interested in our services. We are always ready to serve you. Hopefully, we will be able to gain your trust and satisfaction.
    .
    Our Official Website:- https://hopetechitsolution.com
    .
    Best Regards,
    Mahmud Hasan
    Marketing Executive
    HopeTech-IT Solution
    Email: [email protected]
    WhatsApp Support: +8801614160365

    BobbyPep
    January 22, 2022

    canada drugs online [url=https://disvaiza.mystrikingly.com/#]national pharmacies [/url]
    pharmacies shipping to usa pharmacies online
    pharmacies online https://disvaiza.mystrikingly.com/

    BobbyPep
    February 5, 2022

    pharmacy online cheap [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]medical pharmacy [/url]
    canadian online pharmacy canadian pharmacy online
    online drugstore https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 6, 2022

    cheap prescription drugs [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]canadian pharmacy cialis 20mg [/url]
    pharmacies online canada pharmaceutical online ordering
    canada pharmaceuticals online generic https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 6, 2022

    online drugstore pharmacy [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]best online international pharmacies [/url]
    canadian pharmacy cialis 20mg international pharmacy
    canadian drugstore https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 6, 2022

    apollo pharmacy online [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]canadian pharcharmy online [/url]
    mexican pharmacies international pharmacy
    canadian viagra https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 7, 2022

    canadian pharmacies-247 [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]canadian drugs [/url]
    navarro pharmacy miami pharmacy online shopping
    canadian drugs pharmacies online https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 7, 2022

    canadian drugs pharmacy [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]canadian drugs [/url]
    canadian viagra online medicine to buy
    canadian cialis https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 8, 2022

    viagra generic online pharmacy [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]london drugs canada [/url]
    cialis generic pharmacy online canadian drugs online pharmacies
    canadian pharmacy online viagra https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 9, 2022

    canada pharmaceuticals online generic [url=https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg#]canadian pharmacy review [/url]
    pharmacy discount pharmacies online
    canada pharmacy https://61fe252e95052.site123.me/blog/canadian-pharmaceuticals-online-viagra-dosages-25mg-50mg-100mg

    BobbyPep
    February 13, 2022

    walmart pharmacy viagra [url=https://medicine-online.yolasite.com/#]national pharmacies [/url]
    canadian pharmacies online canadian prescriptions online
    medical pharmacy https://medicine-online.yolasite.com/

    BobbyPep
    February 14, 2022

    pills viagra pharmacy 100mg [url=https://medicine-online.yolasite.com/#]pharmacie [/url]
    canadian drugs pharmacy canadia online pharmacy
    canadian prescriptions online https://medicine-online.yolasite.com/

    BobbyPep
    February 14, 2022

    canadian pharcharmy [url=https://medicine-online.yolasite.com/#]canadian pharmaceuticals online [/url]
    canadian pharmacy review drugstore online
    canadian pharmacy drugs online https://medicine-online.yolasite.com/

    BobbyPep
    February 15, 2022

    mexican pharmacies [url=https://medicine-online.yolasite.com/#]canadian pharmacy online [/url]
    buy generic viagra online pharmacy in canada
    canada discount drug https://medicine-online.yolasite.com/

    BobbyPep
    February 16, 2022

    canadian online pharmacies legitimate [url=https://medicine-online.yolasite.com/#]canadian pharmacies online [/url]
    canadian online pharmacy pharmacy in canada
    cheap pharmacy online https://medicine-online.yolasite.com/

    BobbyPep
    February 17, 2022

    online pharmacies [url=https://medicine-online.yolasite.com/#]canada pharmacy [/url]
    canadian online pharmacies legitimate online pharmacies canada
    24 hour pharmacy https://medicine-online.yolasite.com/

    BobbyPep
    February 18, 2022

    pharmacy online prescription [url=https://medicine-online.yolasite.com/#]online medicine shopping [/url]
    walmart pharmacy online online order medicine
    compound pharmacy https://medicine-online.yolasite.com/

    telegra.phTechnology-A-linchpin-To-Maintain-Businesses-Handle-Remote-Workforce-Amid-Pandemic---Technology-02-16
    February 20, 2022

    tadalafil 20mg https://telegra.ph/Technology-A-linchpin-To-Maintain-Businesses-Handle-Remote-Workforce-Amid-Pandemic---Technology-02-16

    Kudos. Fantastic stuff.

    BobbyPep
    February 21, 2022

    canada drugs pharmacy [url=https://graph.org/Understand-COVID-19-And-Know-The-Tricks-To-Avoid-It-From-Spreading---Medical-Services-02-21#]canadian pharmacy online [/url]
    generic viagra online walmart pharmacy viagra
    national pharmacies https://telegra.ph/Technology-A-linchpin-To-Maintain-Businesses-Handle-Remote-Workforce-Amid-Pandemic---Technology-02-16

    BobbyPep
    February 22, 2022

    online pharmacy drugstore [url=https://telegra.ph/Technology-A-linchpin-To-Maintain-Businesses-Handle-Remote-Workforce-Amid-Pandemic---Technology-02-16#]canadian pharcharmy [/url]
    canadian pharmacy king mexican border pharmacies
    pharmacy online cheap https://telegra.ph/Technology-A-linchpin-To-Maintain-Businesses-Handle-Remote-Workforce-Amid-Pandemic---Technology-02-16

    BobbyPep
    February 23, 2022

    cialis generic pharmacy online [url=https://graph.org/Understand-COVID-19-And-Know-The-Tricks-To-Avoid-It-From-Spreading---Medical-Services-02-21#]canadian online pharmacies [/url]
    canadian pharmacies online pharmacy
    national pharmacies https://telegra.ph/Technology-A-linchpin-To-Maintain-Businesses-Handle-Remote-Workforce-Amid-Pandemic---Technology-02-16

    BobbyPep
    February 24, 2022

    generic viagra online pharmacy [url=http://andere.strikingly.com/#]canadian pharcharmy [/url]
    canada online pharmacies order medicine online
    canadian pharmacy cialis 20mg http://andere.strikingly.com/

    BobbyPep
    February 24, 2022

    aarp recommended canadian pharmacies [url=https://graph.org/Omicron-Variant-Symptoms-Is-An-Excessive-Amount-Of-Mucus-A-COVID-19-Symptom-02-24#]online pharmacy canada [/url]
    national pharmacies online pharmacie
    pharmacy intern https://graph.org/Omicron-Variant-Symptoms-Is-An-Excessive-Amount-Of-Mucus-A-COVID-19-Symptom-02-24

    BobbyPep
    February 25, 2022

    discount pharmacies [url=https://telegra.ph/How-Has-The-COVID-19-Pandemic-Changed-Our-Lives-Globally-02-24#]drugstore online [/url]
    canadian pharmacies online medicine shopping
    national pharmacies online https://telegra.ph/How-Has-The-COVID-19-Pandemic-Changed-Our-Lives-Globally-02-24

Related Posts:

best-css-frameworks
CSS

Best CSS Frameworks in 2021

1. Bootstrap 2. Foundation 3. Bulma 4. Tailwind CSS 5. UI Kit 6. Milligram 7. Materialize CSS Bootstrap Quickly design and customize responsive mobile-first sites

music-player
JavaScript

Music Player With Vanilla JavaScript

In this blog tutorial, we are going to set up and build a music player with vanilla JavaScript from scratch ‘without audio tag’, you can

append-vs-appendChild
JavaScript

Append and AppendChild in JavaScript

The append and appendchild Methods are too popular and used to add elements into the Document Object Model(DOM). But there is a main difference between