Sunday, 9 December 2018

JavaScript: method vs function

A method is a function that is property of an object.

HelloWorld.js

var employee = {
    name : "krishna",
    age : 29,
    toString: function(){
        return "name : " + this.name + ", age : " + this.age + "]";
    }
}

function square(num){
    return num * num;
}

console.log(employee);
console.log(`Square of 10 is ${square(10)}`);

In the above example, toString() is a method, whereas square is a function.

Previous                                                 Next                                                 Home

No comments:

Post a Comment