SlideShare a Scribd company logo
Functions in JavaScript
Dhananjay Kumar [@debug_mode]
Delhi Chapter Lead
Microsoft MVP
Mindcracker MVP
Telerik Evangelist
http://debugmode.net
FB: Dhananjay.25july@gmail.com
Type of function
JavaScript
Functions
Named
Function
Anonymous
Function
Anonymous functions
should be assigned to a
variable
Nested functions
mainfunction(7, 6);
function mainfunction(a, b) {
alert(a);
nestedfunction(88);
function nestedfunction(c) {
alert(b);
};
};
Nested function can access variable of parent
function
Parent function cannot access variable of
nested function
You cannot call nestedfunction from anywhere
but the function it is nested within.
functions in JavaScript
 You can store it in a variable
You can pass it as parameter of
other function
You can use it in an expression
functions in JavaScript
Pass by Value
var greetingmessage = "Hey DJ";
function showmessage(greetingmessage)
{
greetingmessage = "I am changing
value";
}
showmessage(greetingmessage);
alert(greetingmessage);
Pass By Reference
var arrayofname = ["dj"];
function showmessage(arrayofname) {
arrayofname[0] = "dhananjay";
}
showmessage(arrayofname);
alert(arrayofname[0]);
Invocation patterns in JavaScript
Invoking
functions
As functions As methods As constructors
Indirectly
through call()
and apply()
Method invocation pattern
Functions constructor in JavaScript
• Parameters are passed as string in
Function constructor
• Last parameter is always body of the
function
• Body of the function is also in string
• Statements in body are separated
with semicolon
• If there is no input parameter then
body of function would be first and
only parameter in constructor
• Any number of arguments can be
passed in Function constructor
var add = new Function
(
"a",
"b",
"return a+ b; "
);
var abc = add(7, 9);
console.log(abc);
Functions constructor in JavaScript
• It always complies as top level
function
• It creates and complies a JavaScript
function dynamically at run time
• Each time it creates a new object.
So creating function using
constructor in a loop is not
recommended and it is inefficient
var message = "Hello All";
function Data() {
var message = "Hello Data";
return new Function("return message")
;
}
var result = Data()();
alert(result);
functions as value
• JavaScript function can be
assigned to a variable
• JavaScript function can set as
property of an Object
• JavaScript function can be
passed as argument to other
function
• JavaScript function can be
returned from function etc…
function FindGrade(e) {
if (e > 60)
return "Grade A"
else
return "Grade B"
}
var abc = FindGrade;
var result1 = FindGrade(20);
console.log(result1);
var result2 = abc(70);
console.log(result2);
Optional parameter in functions
Does not do any type checking
on argument values
Does not do any checking on
number of arguments passed
JavaScript function can be called
1. With exact number of
arguments
2. With more number of
arguments than specified
3. With less number of
arguments than specified
Optional parameter in functions
Less Number of Arguments
PrintValues(20, 30);
function PrintValues(a, b,c) {
console.log(a + b);
console.log(c);
}
Optional parameter in functions
• While passing less number
of arguments, you handle
undefined either using if or
|| operator.
• Always pass optional
parameter as last argument.
Optional parameter in functions
More numbers of Arguments
PrintValues(20, 30,40,50,60);
function PrintValues(a, b,c) {
if (arguments.length > 3) {
throw Error("invalid arguments");
}
console.log(a);
console.log(c);
var abc = arguments[4];
console.log(abc);
}
Functions in JavaScript
Namespace in JavaScript
Global if defined outside
any function
Local to function and all
nested function , if
defined in function
var MyModule = {
//code for module here
GetData: function () {
var student =
{
name: "dj",
grade: 10
};
var nameisobjectofstudent = "toString" in student;
alert(nameisobjectofstudent);
}
};
Functions in JavaScript
Namespace in JavaScript
Global if defined outside
any function
Local to function and all
nested function , if
defined in function
var MyModule = {
//code for module here
GetData: function () {
var student =
{
name: "dj",
grade: 10
};
var nameisobjectofstudent = "toString" in student;
alert(nameisobjectofstudent);
}
};
Functions in JavaScript
Namespace in JavaScript
Global if defined outside
any function
Local to function and all
nested function , if
defined in function
var studentObject =
{
name: "dj",
marks: 89,
findgrade: function (marks) {
if (marks > 75) {
return "Grade A ";
}
else {
return "Grade B ";
}
}
}
var grade = studentObject.findgrade(99);
alert(grade);
Functions in JavaScript
 It takes a function as argument
 It returns a function as output.
Functions in JavaScript
Less Number of Arguments

More Related Content

PPTX
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 
PDF
JavaScript Functions
Colin DeCarlo
 
PPT
JavaScript Functions
Brian Moschel
 
PDF
Functional Javascript
guest4d57e6
 
PDF
Functional Programming in JavaScript
Will Livengood
 
PDF
Functional Programming with JavaScript
WebF
 
PPTX
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
PPTX
Functional programming in JavaScript
Joseph Smith
 
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 
JavaScript Functions
Colin DeCarlo
 
JavaScript Functions
Brian Moschel
 
Functional Javascript
guest4d57e6
 
Functional Programming in JavaScript
Will Livengood
 
Functional Programming with JavaScript
WebF
 
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Functional programming in JavaScript
Joseph Smith
 

What's hot (20)

PPT
C++ Function
Hajar
 
PPTX
Function C++
Shahzad Afridi
 
PDF
Currying and Partial Function Application (PFA)
Dhaval Dalal
 
PPTX
Javascript Function
xxbeta
 
PPTX
Inline Functions and Default arguments
Nikhil Pandit
 
PPTX
Inline function
Tech_MX
 
PPT
Function overloading(C++)
Ritika Sharma
 
PPTX
Functions in C++
home
 
PPTX
Function overloading
Selvin Josy Bai Somu
 
PDF
Cocoa heads 09112017
Vincent Pradeilles
 
PDF
03 function overloading
Jasleen Kaur (Chandigarh University)
 
PPT
Unit 6 pointers
George Erfesoglou
 
PDF
Extend GraphQL with directives
Greg Bergé
 
PPT
Functions in C++
Mohammed Sikander
 
PPTX
Functions in c++
HalaiHansaika
 
PDF
DRYing to Monad in Java8
Dhaval Dalal
 
PPTX
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
PPTX
Java script – basic auroskills (2)
BoneyGawande
 
PDF
Functions in C++
Pranali Chaudhari
 
PDF
Monads in Swift
Vincent Pradeilles
 
C++ Function
Hajar
 
Function C++
Shahzad Afridi
 
Currying and Partial Function Application (PFA)
Dhaval Dalal
 
Javascript Function
xxbeta
 
Inline Functions and Default arguments
Nikhil Pandit
 
Inline function
Tech_MX
 
Function overloading(C++)
Ritika Sharma
 
Functions in C++
home
 
Function overloading
Selvin Josy Bai Somu
 
Cocoa heads 09112017
Vincent Pradeilles
 
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Unit 6 pointers
George Erfesoglou
 
Extend GraphQL with directives
Greg Bergé
 
Functions in C++
Mohammed Sikander
 
Functions in c++
HalaiHansaika
 
DRYing to Monad in Java8
Dhaval Dalal
 
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
Java script – basic auroskills (2)
BoneyGawande
 
Functions in C++
Pranali Chaudhari
 
Monads in Swift
Vincent Pradeilles
 
Ad

Similar to Java script (20)

PPTX
Functions and Objects in JavaScript
Dhananjay Kumar
 
PPTX
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
PPTX
Javascript functions
Alaref Abushaala
 
PPTX
Lecture 4- Javascript Function presentation
GomathiUdai
 
PPTX
Object oriented java script
vivek p s
 
PDF
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
PPT
JavaScript Introductin to Functions
Charles Russell
 
PPT
Basic Javascript
Bunlong Van
 
PPTX
Advanced Javascript
Dhruvin Shah
 
PPTX
Java script function
suresh raj sharma
 
PPTX
javascript function ujjwal matoliya.pptx
ujjwalmatoliya
 
PDF
java script functions, classes
Vijay Kalyan
 
PPTX
Java script functions
chauhankapil
 
PPTX
Javascript analysis
Uchitha Bandara
 
PPTX
JavaScript- Functions and arrays.pptx
Megha V
 
PPT
JavaScript - Programming Languages course
yoavrubin
 
PDF
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
PPT
25-functions.ppt
JyothiAmpally
 
PDF
Javascript scoping
Aditya Gaur
 
Functions and Objects in JavaScript
Dhananjay Kumar
 
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
Javascript functions
Alaref Abushaala
 
Lecture 4- Javascript Function presentation
GomathiUdai
 
Object oriented java script
vivek p s
 
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
JavaScript Introductin to Functions
Charles Russell
 
Basic Javascript
Bunlong Van
 
Advanced Javascript
Dhruvin Shah
 
Java script function
suresh raj sharma
 
javascript function ujjwal matoliya.pptx
ujjwalmatoliya
 
java script functions, classes
Vijay Kalyan
 
Java script functions
chauhankapil
 
Javascript analysis
Uchitha Bandara
 
JavaScript- Functions and arrays.pptx
Megha V
 
JavaScript - Programming Languages course
yoavrubin
 
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
25-functions.ppt
JyothiAmpally
 
Javascript scoping
Aditya Gaur
 
Ad

More from Dhananjay Kumar (20)

PPTX
Slides of webinar Kendo UI and Knockout.js
Dhananjay Kumar
 
PPTX
Nodejsvs
Dhananjay Kumar
 
PPTX
Node.js
Dhananjay Kumar
 
PPTX
No SQL with Kendo UI
Dhananjay Kumar
 
PPTX
Patterns in JavaScript
Dhananjay Kumar
 
PPTX
Presenter deck icenium hol
Dhananjay Kumar
 
PPTX
Bringbestoinyou
Dhananjay Kumar
 
PPTX
Windows azure mobile service
Dhananjay Kumar
 
PPTX
Test studiowebinaraugcodedstep
Dhananjay Kumar
 
PPTX
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Dhananjay Kumar
 
PPTX
Cloud Based Enterprise Apps using Everlive
Dhananjay Kumar
 
PPTX
A Look into Automated Web UI Test
Dhananjay Kumar
 
PPTX
Windows phone 8 app using Kendo UI
Dhananjay Kumar
 
PPTX
Cross platformmobileapp
Dhananjay Kumar
 
PPTX
Windows aazuremobileservices
Dhananjay Kumar
 
PPTX
Rad controlforwindows25thapril
Dhananjay Kumar
 
PPTX
Data asservice
Dhananjay Kumar
 
PPTX
WCF for begineers
Dhananjay Kumar
 
PPTX
Windows storemindcrcaker23rdmarch
Dhananjay Kumar
 
PPTX
Test studio webinar march 2013
Dhananjay Kumar
 
Slides of webinar Kendo UI and Knockout.js
Dhananjay Kumar
 
Nodejsvs
Dhananjay Kumar
 
No SQL with Kendo UI
Dhananjay Kumar
 
Patterns in JavaScript
Dhananjay Kumar
 
Presenter deck icenium hol
Dhananjay Kumar
 
Bringbestoinyou
Dhananjay Kumar
 
Windows azure mobile service
Dhananjay Kumar
 
Test studiowebinaraugcodedstep
Dhananjay Kumar
 
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Dhananjay Kumar
 
Cloud Based Enterprise Apps using Everlive
Dhananjay Kumar
 
A Look into Automated Web UI Test
Dhananjay Kumar
 
Windows phone 8 app using Kendo UI
Dhananjay Kumar
 
Cross platformmobileapp
Dhananjay Kumar
 
Windows aazuremobileservices
Dhananjay Kumar
 
Rad controlforwindows25thapril
Dhananjay Kumar
 
Data asservice
Dhananjay Kumar
 
WCF for begineers
Dhananjay Kumar
 
Windows storemindcrcaker23rdmarch
Dhananjay Kumar
 
Test studio webinar march 2013
Dhananjay Kumar
 

Recently uploaded (20)

PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 

Java script

  • 1. Functions in JavaScript Dhananjay Kumar [@debug_mode] Delhi Chapter Lead Microsoft MVP Mindcracker MVP Telerik Evangelist http://debugmode.net FB: [email protected]
  • 3. Nested functions mainfunction(7, 6); function mainfunction(a, b) { alert(a); nestedfunction(88); function nestedfunction(c) { alert(b); }; }; Nested function can access variable of parent function Parent function cannot access variable of nested function You cannot call nestedfunction from anywhere but the function it is nested within.
  • 4. functions in JavaScript  You can store it in a variable You can pass it as parameter of other function You can use it in an expression
  • 5. functions in JavaScript Pass by Value var greetingmessage = "Hey DJ"; function showmessage(greetingmessage) { greetingmessage = "I am changing value"; } showmessage(greetingmessage); alert(greetingmessage); Pass By Reference var arrayofname = ["dj"]; function showmessage(arrayofname) { arrayofname[0] = "dhananjay"; } showmessage(arrayofname); alert(arrayofname[0]);
  • 6. Invocation patterns in JavaScript Invoking functions As functions As methods As constructors Indirectly through call() and apply()
  • 8. Functions constructor in JavaScript • Parameters are passed as string in Function constructor • Last parameter is always body of the function • Body of the function is also in string • Statements in body are separated with semicolon • If there is no input parameter then body of function would be first and only parameter in constructor • Any number of arguments can be passed in Function constructor var add = new Function ( "a", "b", "return a+ b; " ); var abc = add(7, 9); console.log(abc);
  • 9. Functions constructor in JavaScript • It always complies as top level function • It creates and complies a JavaScript function dynamically at run time • Each time it creates a new object. So creating function using constructor in a loop is not recommended and it is inefficient var message = "Hello All"; function Data() { var message = "Hello Data"; return new Function("return message") ; } var result = Data()(); alert(result);
  • 10. functions as value • JavaScript function can be assigned to a variable • JavaScript function can set as property of an Object • JavaScript function can be passed as argument to other function • JavaScript function can be returned from function etc… function FindGrade(e) { if (e > 60) return "Grade A" else return "Grade B" } var abc = FindGrade; var result1 = FindGrade(20); console.log(result1); var result2 = abc(70); console.log(result2);
  • 11. Optional parameter in functions Does not do any type checking on argument values Does not do any checking on number of arguments passed JavaScript function can be called 1. With exact number of arguments 2. With more number of arguments than specified 3. With less number of arguments than specified
  • 12. Optional parameter in functions Less Number of Arguments PrintValues(20, 30); function PrintValues(a, b,c) { console.log(a + b); console.log(c); }
  • 13. Optional parameter in functions • While passing less number of arguments, you handle undefined either using if or || operator. • Always pass optional parameter as last argument.
  • 14. Optional parameter in functions More numbers of Arguments PrintValues(20, 30,40,50,60); function PrintValues(a, b,c) { if (arguments.length > 3) { throw Error("invalid arguments"); } console.log(a); console.log(c); var abc = arguments[4]; console.log(abc); }
  • 15. Functions in JavaScript Namespace in JavaScript Global if defined outside any function Local to function and all nested function , if defined in function var MyModule = { //code for module here GetData: function () { var student = { name: "dj", grade: 10 }; var nameisobjectofstudent = "toString" in student; alert(nameisobjectofstudent); } };
  • 16. Functions in JavaScript Namespace in JavaScript Global if defined outside any function Local to function and all nested function , if defined in function var MyModule = { //code for module here GetData: function () { var student = { name: "dj", grade: 10 }; var nameisobjectofstudent = "toString" in student; alert(nameisobjectofstudent); } };
  • 17. Functions in JavaScript Namespace in JavaScript Global if defined outside any function Local to function and all nested function , if defined in function var studentObject = { name: "dj", marks: 89, findgrade: function (marks) { if (marks > 75) { return "Grade A "; } else { return "Grade B "; } } } var grade = studentObject.findgrade(99); alert(grade);
  • 18. Functions in JavaScript  It takes a function as argument  It returns a function as output.
  • 19. Functions in JavaScript Less Number of Arguments