• Wednesday , 22 November 2017

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/functions-in-javascript.html

A function is a block of reusable code. A function allows us to write code once and use it as many times as we want just by calling the function.

JavaScript function syntax
function functionName(parameter1, parameter2,..parameter_n)
{
//function statements
}

Points to remember
1. Use the function keyword to define a function, followed by the name of the function. The name of the function should be followed by parentheses ().
2. Function parameters are optional. The parameter names must be with in parentheses separated by commas.

Example : JavaScript function to add 2 numbers. The following JavaScript function adds 2 numbers and return the sum

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

Calling the JavaScript function : Call the JavaScript function by specifying the name of the function and values for the parameters if any.

var sum = addNumbers(10, 20);
alert(sum);

Output : 30

What happens when you do not specify values for the function parameters when calling the function
The parameters that are missing values are set to undefined

Example : In the example below, we are passing 10 for the firstNumber parameter but the secondNumber parameter is missing a value, so this parameter will be set to undefined. When a plus (+) operator is applied between 10 and undefined we get not a number (NaN) and that will be alerted.

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

var sum = addNumbers(10);
alert(sum);

Output : NaN

What happens when you specify too many parameter values when calling the function.
The extra parameter values are ignored.

Example : In the example below, 30 & 40 are ignored.

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

var sum = addNumbers(10, 20, 30, 40);
alert(sum);

Should a javascript function always return a value
No, they don’t have to. It totally depends on what you want the function to do. If an explicit return is omitted, undefined is returned automatically. Let’s understand this with an example.

The following function returns the sum of two numbers. We are storing the return value of the function in sum variable and writing it’s value to the document.

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

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

The following function does not return any value. It simply writes the sum of two numbers to the page. However, we are assigning the return value of addNumbers() function to sum variable. Since addNumbers() function in this case does not have an explicit return statement, undefined will be returned.

function addNumbers(firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
document.write(result);
}

var sum = addNumbers(10, 20);
alert(sum);

Original source

3d Ocean

Related Posts

9 Comments

  1. shaheem shan. P
    July 10, 2017 at 06:03

    Very informative .
    Thank you sir.

  2. Adrian Alvarado
    July 10, 2017 at 06:03

    Well done, thanks.

  3. Carl Argabright
    July 10, 2017 at 06:03

    Thanks man! Very simple but informative and well done!

  4. mehdi hussain
    July 10, 2017 at 06:03

    thnx for such an amazing tutorial. sir i just need to know that we only can assign the return value of the function to the global variable?

  5. Faizan Gaima
    July 10, 2017 at 06:03

    Like the way you explained… you have great quality sir. keep going!!! and request you to please upload a tutorial on Jquery. Thank you šŸ™‚

  6. mahipal kamanchi
    July 10, 2017 at 06:03

    thank u sir….!

  7. Vishnu Kanth
    July 10, 2017 at 06:03

    Thanks

  8. Abhishek Gupta
    July 10, 2017 at 06:03

    thanks a lot

  9. Leamsi Ordnavi
    July 10, 2017 at 06:03

    Thanks sir šŸ™‚

Leave A Comment

You must be logged in to post a comment.