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
Views:
214
28 Comments on JavaScript Difference between Methods and Functions
Nice and simple... 7 views... I came all the way from dev Community
Thanks bro!🌹
Nice and short
Useful and short article.
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
canada drugs online [url=https://disvaiza.mystrikingly.com/#]national pharmacies [/url]
pharmacies shipping to usa pharmacies online
pharmacies online https://disvaiza.mystrikingly.com/
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
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
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
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
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
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
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
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
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/
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/
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/
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/
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/
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/
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/
tadalafil 20mg https://telegra.ph/Technology-A-linchpin-To-Maintain-Businesses-Handle-Remote-Workforce-Amid-Pandemic---Technology-02-16
Kudos. Fantastic stuff.
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
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
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
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/
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
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