• Monday , 11 December 2017

Different ways of defining functions in JavaScript

Code Canyon



Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2014/12/different-ways-of-defining-functions-in.html

In JavaScript, there are several different ways of defining functions.

Defining a function using function declaration

Example 1 : Declaring a function first and then calling it.

function addNumbers(firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}

var sum = addNumbers(10, 20);
document.write(sum);

Output : 30

Example 2 : A function call is present before the respective function declaration

In Example 1, we are first defining the function and then calling it. The call to a JavaScript function can be present anywhere, even before the function is declared. The following code also works just fine. In the example below, we are calling the function before it is declared.

var sum = addNumbers(10, 20);
document.write(sum);

function addNumbers(firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}

Function Hoisting : By default, JavaScript moves all the function declarations to the top of the current scope. This is called function hoisting. This is the reason JavaScript functions can be called before they are declared.

Defining a JavaScript function using a function expression : A Function Expression allows us to define a function using an expression (typically by assigning it to a variable). There are 3 different ways of defining a function using a function expression.

Anonymous function expression example : In this example, we are creating a function without a name and assigning it to variable add. We use the name of the variable to invoke the function.

var add = function (firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}

var sum = add(10, 20);
document.write(sum);

Functions defined using a function expression are not hoisted. So, this means a function defined using a function expression can only be called after it has been defined while a function defined using standard function declaration can be called both before and after it is defined.

// add() is undefined at this stage
var sum = add(10, 20);
document.write(sum);

var add = function (firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}

Named function expression example : This is similar to the example above. The difference is instead of assigning the variable to an anonymous function, we’re assigning it to a named function (with the name computeFactorial).

var factorial = function computeFactorial(number)
{
if (number [= 1)
{
return 1;
}

return number * computeFactorial(number – 1);
}

var result = factorial(5);
document.write(result);

The name of the function (i.e computeFactorial) is available only with in the same function. This syntax is useful for creating recursive functions. If you use computeFactorial() method outside of the function it raises ‘computeFactorial’ is undefined error.

var factorial = function computeFactorial(number)
{
if (number [= 1)
{
return 1;
}

return number * computeFactorial(number – 1);
}

var result = computeFactorial(5);
document.write(result);

Output : Error – ‘computeFactorial’ is undefined.

Self invoking function expression example :

var result = (function computeFactorial(number)
{
if (number [= 1)
{
return 1;
}

return number * computeFactorial(number – 1);
})(5);

document.write(result);

Output : 120

These are called with different names
Immediately-Invoked Function Expression (IIFE)
Self-executing anonymous functions
Self-invoked anonymous functions

Original source

3d Ocean

Related Posts

20 Comments

  1. StyleTrick
    October 9, 2017 at 23:04

    Thank you Kudvenkat! These videos are extremely helpful!

  2. aziz as
    October 9, 2017 at 23:04

    good job.

  3. The Shroud
    October 9, 2017 at 23:04

    Could you please tell me how many different types of functions declarations there are in Javascript?

  4. Horsegirl505
    October 9, 2017 at 23:04

    You did an excellent job of explaining these concepts. I even understood them and I'm a beginner in javascript. I like what your doing!

  5. Sean Rasmussen
    October 9, 2017 at 23:04

    great vid — really helped clarify the different types of functions

  6. Sahil Kumar
    October 9, 2017 at 23:04

    Thanks Kudvenkat! Videos are very nice. It gave me the confidence to clear any interview. I am learning a lot from your videos but I am not able to use that for practical situations. Like why we need hoisting? I am understanding concept but not getting why and where we use these things in real world

  7. yhteensatuma7
    October 9, 2017 at 23:04

    why it shows error when i remove if(number <= 1) {return 1}.

  8. ng- Course
    October 9, 2017 at 23:04

    illustrious video

  9. bra .Scene
    October 9, 2017 at 23:04

    Thanks!!

  10. Claudio Narra
    October 9, 2017 at 23:04

    Thank you very much !

  11. Sujeet Pasi
    October 9, 2017 at 23:04

    Thank your so much sir for explaining in JavaScript braces does not have any scope

  12. David S. Huang
    October 9, 2017 at 23:04

    THANK YOU SO MUCH for explaining the third self-invoking function method!!

  13. Manoj Mishra
    October 9, 2017 at 23:04

    Thanks sir,
    when u r uploading the angular JS tutorials.

  14. Thomas Butman
    October 9, 2017 at 23:04

    Thanks for another great video, Kudvenkat! One question on your immediately-invoked function expression–I noticed you used the syntax (function(number){…}(5)). On your next slide you used (function(number){…})(5). In one case the function expression is wrapped in parentheses, and then the argument passed to the function follows. In the other case, the entire function expression along with the argument are wrapped in outer parentheses. Do both cases represent valid syntax? Thanks!

  15. Truman Kwong
    October 9, 2017 at 23:04

    thanks. was able to answer an interview question from watching this.

  16. Abhishek Gupta
    October 9, 2017 at 23:04

    thanks a lot for you knowledge sharing session.

  17. Abhishek Gupta
    October 9, 2017 at 23:04

    Please continue your example

  18. Sudeep Makwana
    October 9, 2017 at 23:04

    THanks for video bro .. its helps a lot .. 🙂 cheers !!!

  19. Lars Rye Jeppesen
    October 9, 2017 at 23:04

    Thank you

  20. mahipal kamanchi
    October 9, 2017 at 23:04

    thank u sir…!

Leave A Comment

You must be logged in to post a comment.