Function Constructor Declaration in javascript examples

javascript Function Declaration

Function Object examples

Javascript is programming language which is unique features compared to another programming language. Function object construction is one of the cool features.

Functions will be created using the name and function keyword.

Functions will be helpful to execute the repetitive code and avoids duplicate code writing.

Function features

1. Will be created using task processing

2. Returns data to the caller

3. Will take parameters

4. This is objects in javascript

###Functional Declaration Syntax

function nameoffunction(parameters){  
return something or void}  

There are many ways we can create a function object using declaration

Simple functional declaration

It can be created using function keyword and function name

example is

Function add(a,b){  
return a+b;  
add(3,5); returns 8 as output  

Functional expression example

This is another way of creation function object

let addFunction=function(a,b){  
return a+b  
addFunction(2,4) returns 6 as output  

Array Function expression examples 

It also creates functions without a name. This is shorter syntax and introduced in latest javascript language

let addF=(a,b){  
return a+b}  
addF(4,5) returns 9 as output  

Using Function Constructor Object 

This is an alternative way of creating Function object In a javascript any object can be created using the new operator, For number - new number. In the same way, Function Object is created using the new Function() method boolean, String, Date are some of the primitive types. remaining everything are objects.

new Function({arguments }, body)  

Parameters are argument are parameters to Function object. Body is javascript expression enclosed in quotes
Function Construtor exmaple var add = new Function(); console.log(add) //returns nothing console.log( add(2,7)) //returns undefined

var add1 = new Function('p', 'q', 'return p + q');  
console.log(typeof add) //returns 'function'  
console.log( add1(2,7)) //returns 9 

Using Function Constructor and function declaration keyword both provides the same functionality and difference is in terms of syntax.
Function inherits from Function.prototype which inherits from Object.prototype. Object.prototype is a superclass for all classes So the function can reuse properties and methods from available in Function.prototype and Object Function Constructor and Function Declaration has some differences in terms of local and global scope of Function Constructor is to use global context and return the value not from where it is being called. whereas function constructor has the scope of local where it is declared

var value = 50;  
function FunctionDeclaration1() {  
    var value = 30;  
    return new Function('return value;'); // value is returned from global variable value  
function FunctionDeclaration2() {  
    var value = 60;  
    function innerfucntion() {  
        return value; //   this returns local variable value  
    return innerfucntion;  
var function1 = FunctionDeclaration1();  
console.log(function1());          // 50  
var function2 = FunctionDeclaration2();  
console.log(function2());          // 60  

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.