0% found this document useful (0 votes)
10 views10 pages

JavaScript Functions: Named, Anonymous, Nested

notes

Uploaded by

2k23.mca2313345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

JavaScript Functions: Named, Anonymous, Nested

notes

Uploaded by

2k23.mca2313345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVASCRIPT

1
JavaScript Function
❑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.

❑Java script function can be used using 3 way :


1.Named Function
2.Anonymous function
3.Nested Function
2
JavaScript NamedFunction
❑ 1.Named Functions: In JavaScript, named functions are simply a way of referring to a
function that employs the function keyword followed by a name that can be used as a
callback to that function. Normal functions with a name or identifier are known as
named functions

JavaScript Named 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.
3
Calling JS Named Function
▪ JavaScript function to add 2 numbers. The following JavaScript
function adds 2 numbers and return the sum.
<script>
function addNumbers(firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result;
}
var sum = addNumbers(10, 20);

document.write("<b><h1>Addition of 2 number is<b><h1> "+sum);


</script>

4
2.Anonymous function
• A Function without name is called Anonymous function.

• As the name suggests, “anonymous function” refers to a


function that does not have a name or a title.

• In JavaScript, an anonymous function is something that is


declared without an identification.

5
Anonymous function
• 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.

<script>
var add = function (firstNumber, secondNumber)
{
var result = firstNumber + secondNumber;
return result; }
var sum = add(10, 20);
document.write(sum);
</script>
3.Nested or Inner Function
• Function with in function is known as nested
function.
• You may nest a function within another function.
The nested (inner) function is private to its
containing (outer) function.
• nested function can "inherit" the arguments and
variables of its containing function. In other
words, the inner function contains the scope of
the outer function.
3.Nested or Inner Function
function addSquares(a, b)
{
function square(x)
{ Nested Function
return x * x;
}
return square(a) + square(b);
}
3.Nested or Inner Function
<script type="text/javascript">
function addSquares(a, b)
{
function square(x)
{
return x * x;
}
return square(a) + square(b);
}
const a = addSquares(2, 3); // returns 13
const b = addSquares(3, 4); // returns 25
const c = addSquares(4, 5); // returns 41

document.write("<b>Square Sum<b>"+a);
document.write(" <b>Square Sum<b>"+b);
document.write(" <b>Square Sum<b> "+c);

</script>
Difference between named
function and anonymous function
Anonymous functions Named Functions
In JavaScript, anonymous functions (function Normal functions (function declaration)
expression) are those that are produced having a name or identifier to refer to them
without a name or identifier to refer to them; are referred to as named functions and are
they are defined as : defined as:
function(){ function displayMessage(){
// Function Body // function..body
} }
Anonymous functions are never hoisted Named functions are hoisted (loaded into
(loaded into memory at compilation). memory at compilation).
When invoking an anonymous function, you A name function can be invoked before
can only call it after the declaration line. declaration.

You might also like