Javascript
JS Functions
Diffrence Between Methods and Functions in JS

Understanding the difference between methods and functions in JavaScript is important for writing better code. While both perform tasks, functions are independent, while methods belong to objects. Knowing when to use each helps you write cleaner and more efficient JavaScript programs.

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 the function is enclosed in curly braces.

Syntax:

function functionName(parameters) {
    // Content
}

Work Around:

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

Example:

Below is the function to add two numbers.

function func(a, b) {
    let sum = a + b;
    return sum;
}
console.log(func(1, 2));

Output:

3

JavaScript Methods

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

Syntax:

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

Work Around:

  • Actions that can be performed on objects are called JavaScript methods.
  • The objects can also be called without using parentheses.
  • this refers to the owner object in a method.

Example:

The following example shows a method that is invoked with an object called employee.

let employee = {
    empname: "Rahul",
    department: "sales",
    details: function () {
        return this.empname +
            " works with Department " +
            this.department;
    }
};
console.log(employee.details());

Output:

Rahul works with Department sales

Difference Between Function and Method

FunctionMethod
A JavaScript function is a block of code designed to perform a particular task.A JavaScript method is an object property that has a function value.
Syntax: function functionName(parameters) { // Content }Syntax: object = { methodName: function() { // Content } }; object.methodName()
A function can pass the data that is operated and may return the data.A method operates on the data contained in a class.
Data passed to a function is explicit.A method implicitly passes the object on which it was called.
A function lives on its own.A method is a function associated with an object property.
A function can be called directly by its name.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.
Functions are helpful because they increase the reusability of code.JavaScript includes some in-built methods, such as parseInt().
The () operator is used to invoke a function.We can access an object method using the syntax: objectName.methodName().