Functions in Javascript (Declaration, Expression, Arrow)

There are three ways of using functions and each has its own name, one is called “Function Declarations” and another is “Function Expressions”. The last one to be discussed is “Arrow Function”, which has the shortest syntax for a function.

The Syntactical differences between them are like so :

Function Declaration declared as a separate statement, in the main code flow.

// Function Declaration
function sum(a, b) {
   return a + b;
}
sum(a,b) // calling the function

Function Expression is created inside an expression or inside another syntax construct. The function below is treated like a value which gets assigned into a variable – together when called with ( ), will become an executable function.

// Function Expression
let sum = function(a, b) {
   return a + b;
};
sum(a,b) // calling the function

Arrow function offers a shorter syntax like so :

let sum = (a, b) => a + b; // one line of expression does not require "return" keyword, because it is implicit

If we have only one argument, then parentheses can be omitted, making that even shorter:

let displayA = a => alert("this is ", a);

If there are no arguments, parentheses should be empty (but they should be present):

let sayHi = () => alert("Hello!");

If there is more than one line inside the code block encapsulated by the pair of { } , we would need to use the “return” keyword, like so :

let sum = (a, b) => {
   let result = a + b;
   return result;
};

Now, let’s discuss about the the more subtle difference between the Function Declaration and Function Expression. It has to do with the execution time of the function.

A Function Expression is created when the execution reaches it and is usable from then on.

Once the execution flow passes to the right side of the assignment,

let sum = function(a,b) { ...

at that point, the function is created and can be used thereafter (assigned, called etc).

Function Declarations are different.

A Function Declaration is usable in the whole script/code block.

In other words, when JavaScript prepares to run the script or a code block, it first looks for Function Declarations in it and creates the functions. Much like var that is hoisted; declaring a variable anywhere in the code is equivalent to declaring it at the top.

And after all of the Function Declarations are processed, the execution moves on.

As a result, a function declared as a Function Declaration can be called earlier than it is defined.

For example, this works:

sayHi("Pillow"); // Hello, Pillow 

function sayHi(name) { 
   alert( `Hello, ${name}` ); 
}

The Function Declaration sayHi is created when JavaScript is preparing to start the script and is visible everywhere in it.

If it were a Function Expression, then it wouldn’t work:

sayHi("Pillow"); // Error! 

// so far, the execution has never encountered the sayHi function

let sayHi = function(name) {  // (*) execution happens here and only in lines below, the sayHi can be used

   alert( `Hello, ${name}` );
};

Function Expressions are created when the execution reaches them. That would happen only in the line marked with (*) .

Another post will cover specifically about Arrow function, but the gists about Arrow function are :

  • Does not have its own this (inherit it from its nearest object-bound parent, read more here)
  • Does not have arguments
  • Can’t be called with new
  • Does not have super

79 Comments

  1. StephenLag October 28, 2020 at 9:39 pm

    where can i buy viagra generic viagra viagra for men online

  2. GeorgeVab October 30, 2020 at 3:49 pm

    viagra walgreens buy sildenafil where to get viagra

  3. JeremyJah October 31, 2020 at 10:28 pm

    cialis discount card tadalafil cialis reps

  4. Edwardchece November 2, 2020 at 12:04 am

    cheap viagra online buy viagra order viagra online

  5. RogerRon November 4, 2020 at 8:24 pm

    viagra from india cheap ed pills viagra cost per pill
    viagra without doctor prescription

  6. RogerRon November 5, 2020 at 1:11 pm

    viagra online usa buy viagra generic generic viagra without a doctor prescription
    new erectile dysfunction treatment

  7. RogerRon November 6, 2020 at 6:22 am

    canadian pharmacy viagra generic viagra buy generic viagra online
    homepage

  8. RogerRon November 6, 2020 at 11:28 pm

    cheapest viagra online buy viagra online viagra from canada
    cheap erectile dysfunction

  9. Peterpaymn November 11, 2020 at 12:16 am

    generic cialis bitcoin cheap cialis canada price on cialis

  10. Peterpaymn November 11, 2020 at 4:52 pm

    best liquid cialis cialis for sale the cost of cialis

  11. ThomasSus November 13, 2020 at 10:31 pm

    erection pills online buy ed pills online medicines for ed
    do i have ed

  12. RobertNax November 20, 2020 at 2:00 pm

    cheap tadalafil tadalafil pharmacy

  13. BrianSlutt December 3, 2020 at 11:25 am

    coupons for cialis cialis for sale cialis for peyronie

  14. BrianSlutt December 3, 2020 at 7:27 pm

    cheapest generic viagra cheap viagra canadian pharmacy generic viagra

  15. BrianSlutt December 4, 2020 at 3:21 am

    amoxicillin without a doctor’s prescription Zithromax india pumps for ed

  16. BrianSlutt December 4, 2020 at 1:53 pm

    viagra over the counter viagra pill viagra viagra walmart

  17. BrianSlutt December 4, 2020 at 11:05 pm

    ed pills that really work usa pharmacy india best ed pills

  18. PhilipUnete December 7, 2020 at 4:37 pm

    errection problems: online ed meds ed pills that really work

  19. DanielCrags December 9, 2020 at 7:32 pm

    zithromax online zithromax price canada zithromax over the counter uk

  20. DanielCrags December 10, 2020 at 12:11 pm

    can you buy viagra over the counter viagra cost per pill viagra discount

  21. DanielCrags December 10, 2020 at 8:17 pm

    online doctor prescription for viagra viagra price viagra price comparison

  22. DanielCrags December 11, 2020 at 4:15 am

    side effects for cialis free cialis coffee with cialis

  23. DanielCrags December 11, 2020 at 8:30 pm

    zithromax z-pak price without insurance cost of generic zithromax zithromax coupon

  24. Allenincox December 12, 2020 at 2:44 pm

    where to get viagra generic pills how much will generic viagra cost

  25. Allenincox December 12, 2020 at 10:36 pm

    buy viagra online generic viagra canada no prescription viagra

  26. Allenincox December 13, 2020 at 6:54 am

    viagra without a doctor prescription usa viagra generic for sale viagra without a doctor prescription canada

  27. WilfredGeacy December 15, 2020 at 4:12 pm

    ed for men cheap Aciclovir buy Valtrex online

  28. WilfredGeacy December 16, 2020 at 12:50 am

    buy antibiotics online cheap Amoxil new ed drugs

  29. WilfredGeacy December 16, 2020 at 9:54 am

    Flagyl buy Cenmox canadian drug pharmacy

  30. WilfredGeacy December 16, 2020 at 9:49 pm

    buy prescription drugs without doctor buy Valtrex online order Plaquenil

  31. porno December 17, 2020 at 7:51 pm

    Way cool! Some extremely valid points! I appreciate you penning this article and also the rest of the website is very good. Agata Lance Riedel

  32. HaroldAvage December 18, 2020 at 4:43 am

    100mg viagra without a doctor prescription online pharmacy canada fda approved canadian online pharmacies

  33. HaroldAvage December 18, 2020 at 4:24 pm

    cialis generic generic cialis where to bay cialis (tadalafil) pills 80mg

  34. HaroldAvage December 19, 2020 at 4:34 am

    ed medications comparison cheap lipitor generic lipitor 10 mg tablet

  35. HaroldAvage December 20, 2020 at 4:43 am

    cialis online generic tadalafil buy viagra cialis

  36. JohnnyOpirl December 23, 2020 at 10:10 pm

    valtrex without a prescription acyclovir price where to buy valtrex online

  37. JohnnyOpirl December 25, 2020 at 11:59 am

    amoxicillin 500 mg purchase without prescription order bactrim online doxycycline without prescription

  38. JohnnyOpirl December 26, 2020 at 12:21 am

    buy levlen online alesse pills price buy yasmin online singapore

  39. Davidhinue January 11, 2021 at 5:36 pm

    best natural ed treatment https://canadarx24.com/ prescription drugs online without doctor

  40. Davidlycle January 16, 2021 at 11:04 am

    generic viagra without a doctor prescription https://edcheapgeneric.com/ jiactgdm viagra for men online

  41. Michaelmug January 19, 2021 at 7:54 pm

    canada cialis cialis online brfphnuv how much does cialis cost

  42. Michaelmug January 20, 2021 at 9:06 pm

    what is cialis used for generic cialis tadalafil ipapklqg cialis tadalafil 20 mg

Leave a comment

Your email address will not be published. Required fields are marked *