0% found this document useful (0 votes)
5 views16 pages

2.4 Functions

The document discusses JavaScript functions, focusing on anonymous functions, which are unnamed and can be assigned to variables or passed as arguments. It highlights their usefulness in creating cleaner code, handling callbacks, and maintaining localized scope. Examples illustrate how anonymous functions can be utilized in various contexts, such as being stored in variables and passed to higher-order functions like setTimeout.

Uploaded by

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

2.4 Functions

The document discusses JavaScript functions, focusing on anonymous functions, which are unnamed and can be assigned to variables or passed as arguments. It highlights their usefulness in creating cleaner code, handling callbacks, and maintaining localized scope. Examples illustrate how anonymous functions can be utilized in various contexts, such as being stored in variables and passed to higher-order functions like setTimeout.

Uploaded by

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

PHP & JS FRAMEWORK 1

20CSI504-PHP & JS FRAMEWORK

Module :II
Module name :JavaScript Fundamentals
Topic :Functions
Faculty : Dr. S. Karthikeyini, AP/M.Tech CSE
PHP & JS FRAMEWORK 2

FUNCTIONS
PHP & JS FRAMEWORK 3

TYPES
PHP & JS FRAMEWORK 4

NAMED FUNCTION
PHP & JS FRAMEWORK 5

NUMBER
PHP & JS FRAMEWORK 6

NUMBER
7

What is an Anonymous Function?


• An anonymous function is a function that does not
have a name.
• In JavaScript, functions can be created and assigned to
a variable or passed as an argument without naming
them.
• Anonymous functions are useful for:
• Short-term use (like callbacks).
• Reducing the need for defining a named function.
8

Example 1: Stored in a Variable


• var add = function(a, b) { return a + b; };
console.log(add(2, 3)); // Output: 5
• Explanation:
• function(a, b) → Anonymous function that takes two
parameters (a, b).
• { return a + b; } → Returns the sum of a and b.
• var add = → Assigns the function to the add variable.
• add(2, 3) → Calls the function and returns 5.
• ➡️The function has no name but is referenced by the
add variable.
9

Example 2: Passed as an Argument to Another Function


• setTimeout(function() { alert("this message is displayed after 5
seconds"); }, 5000);
• Explanation:
• setTimeout() → Built-in function that takes two arguments:
• Function → Executes the function after a specified delay.
• 5000 → Delay in milliseconds (5 seconds).
• function() → Anonymous function that displays an alert message.
• ➡️The function is passed directly as an argument to setTimeout()
without needing to give it a name.
10

Key Characteristics of Anonymous Functions:

Feature Description

No Name Anonymous functions don’t have a name.

Stored in Variables They can be assigned to variables.

Often used as arguments for higher-order


Passed as Arguments
functions like setTimeout, map, filter, etc.

Anonymous functions are not hoisted to the top


No Hoisting
of the scope.
11

Why Use Anonymous Functions?


• ✅ Cleaner and shorter code.
✅ Useful for callbacks and event handling.
✅ Keeps the scope localized and avoids polluting the
global namespace.
PHP & JS FRAMEWORK 12

CONSTRUCTOR
PHP & JS FRAMEWORK 13

SELF INVOKING FUNCTION


PHP & JS FRAMEWORK 14

EXAMPLE
PHP & JS FRAMEWORK 15

EXAMPLE
//named function
• function sqr(a)
• {
• return a*a;
• }
• console.log("the invoking named function:" +sqr(2));

• //anonymous function
• var z=function(b)
• {
• return b*b;
• }
• console.log("anonymous function:" +z(2));

• //constructor function
• var q=new Function("v","return v*v;");
• console.log("constructor function:" +q(2));

• //self invoking function

• (function(d)
• {
• console.log("self invoking function:"+ d*d);
• return d*d;
• })(2);
16

You might also like