Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Explain the Different Function States in JavaScript

  • Last Updated : 07 Oct, 2021

In javascript, we can create functions in many different ways according to the need for the specific operation. For example, sometimes we need asynchronous functions or synchronous functions.  In this article, we will discuss the difference between function Person( ) { }, var person = Person ( ), and var person = new Person ( ). 

Function Declaration: The given below syntax is a declaration of the function. The code declares a function statement but does not execute until we don’t call them. Here Person is the name of the function. Functions in the javascript are declared using this method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

function Person (){}

Example of Function Declaration: In this example, we will create a function named with Person and perform some tasks as shown below. 

Javascript






function Person() {
    name = "Vikash";
    age = "22";
}

Function Expression: In this code, a variable person is defined using the var keyword. We can also use const and let keywords. The person variable contains all the value references of the Person function. In javascript, every expression returns a value. The value may be undefined if the function doesn’t return and the value or the value will be the return type of the function. This function could be an anonymous function if the name is not assigned and wrapped in parenthesis interpreted as an expression. 

var person = Person ()

Example of Function Expression:

index.js

Javascript




// Function declaration
function person() { }  
var person = person()
  
// Printing the return value
// of the person() function
console.log(person)
  
function person1(name) {
    return name;
}
  
var person1=person1("Aayush")
  
// Printing the value of person1
console.log(person1)

Run index.js file using below command:

node index.js

Output:

undefined
Aayush

An undefined will print because the function doesn’t return any value whereas the second function returns the name value that’s why the name is printed in the second line.

Function Constructor: In this code, we are creating an instance using the new keyword. Here the person is the instance (object) of the Person. A function declaration is a regular function unless we create an instance of the function. The main advantage of the function constructor is to initialize the member values of the javascript functions.

Once the instance is created, we can use the function by created a variable as shown below example. 

var person = new Person()

index.js

Javascript




// Creating the function
function Person (name, age) {
    this.name = name;
    this.age = age;
}
  
// Calling the function 
var person = new Person("Vikah", 22);
  
console.log(person.name);
console.log(person.age);

Run index.js file using below command:

node index.js

Output:

Vikah
22



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!