Tag Archives: java script functions

Difference between: function Person(){}, var person = Person(), and var person = new Person()?

Difference between: function Person(){}, var person = Person(), and var person = new Person()?

There are three different syntax we can use fordeclaring functionsin java script
i) Function Declaration
function Person(){
console.log(“Hello”);
}

A Function Declaration defines a named function variable without requiring variable assignment. Theyare declared as constructs which are standalone and cannot be nested within non-function blocks.

ii) Function expressions:
var Person = function(){
console.log(“Hello”);
};
A function expression looks similar to function declarations, except that the function is assigned to a variable name. The function name can be optional. If the name isn’t given, then we can term it as an anonymous.

iii) Function constructor:
varmyPerson = new Person();
Such a syntax declaration is used when creating an instance of a Class(Constructor). Here, we pass in an unlimited number of arguments in the front and use the keyword “new”.