SlideShare ist ein Scribd-Unternehmen logo
1 von 19
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

Weitere ähnliche Inhalte

Was ist angesagt?

C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Dhaval Dalal
 
Javascript Function
Javascript FunctionJavascript Function
Javascript Functionxxbeta
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Extend GraphQL with directives
Extend GraphQL with directivesExtend GraphQL with directives
Extend GraphQL with directivesGreg Bergé
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8Dhaval Dalal
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioLuis Atencio
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)BoneyGawande
 

Was ist angesagt? (20)

C++ Function
C++ FunctionC++ Function
C++ Function
 
Function C++
Function C++ Function C++
Function C++
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Javascript Function
Javascript FunctionJavascript Function
Javascript Function
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Inline function
Inline functionInline function
Inline function
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Extend GraphQL with directives
Extend GraphQL with directivesExtend GraphQL with directives
Extend GraphQL with directives
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 

Ähnlich wie Java script

JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashBret Little
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015senejug
 
Type script, for dummies
Type script, for dummiesType script, for dummies
Type script, for dummiessantiagoaguiar
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014hezamu
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#khush_boo31
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptxAshwini Raut
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college2017eee0459
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab FunctionsUmer Azeem
 

Ähnlich wie Java script (20)

JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Type script, for dummies
Type script, for dummiesType script, for dummies
Type script, for dummies
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
4th unit full
4th unit full4th unit full
4th unit full
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab Functions
 

Mehr von Dhananjay Kumar

Slides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.jsSlides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.jsDhananjay Kumar
 
Presenter deck icenium hol
Presenter deck   icenium holPresenter deck   icenium hol
Presenter deck icenium holDhananjay Kumar
 
Windows azure mobile service
Windows azure mobile serviceWindows azure mobile service
Windows azure mobile serviceDhananjay Kumar
 
Test studiowebinaraugcodedstep
Test studiowebinaraugcodedstepTest studiowebinaraugcodedstep
Test studiowebinaraugcodedstepDhananjay Kumar
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript Dhananjay Kumar
 
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile Dhananjay Kumar
 
Cloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using EverliveCloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using EverliveDhananjay Kumar
 
A Look into Automated Web UI Test
A Look into Automated Web UI TestA Look into Automated Web UI Test
A Look into Automated Web UI TestDhananjay Kumar
 
Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI Dhananjay Kumar
 
Windows aazuremobileservices
Windows aazuremobileservicesWindows aazuremobileservices
Windows aazuremobileservicesDhananjay Kumar
 
Rad controlforwindows25thapril
Rad controlforwindows25thaprilRad controlforwindows25thapril
Rad controlforwindows25thaprilDhananjay Kumar
 
Windows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarchWindows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarchDhananjay Kumar
 

Mehr von Dhananjay Kumar (20)

Slides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.jsSlides of webinar Kendo UI and Knockout.js
Slides of webinar Kendo UI and Knockout.js
 
Nodejsvs
NodejsvsNodejsvs
Nodejsvs
 
Node.js
Node.jsNode.js
Node.js
 
No SQL with Kendo UI
No SQL with Kendo UI No SQL with Kendo UI
No SQL with Kendo UI
 
Patterns in JavaScript
Patterns in JavaScriptPatterns in JavaScript
Patterns in JavaScript
 
Presenter deck icenium hol
Presenter deck   icenium holPresenter deck   icenium hol
Presenter deck icenium hol
 
Bringbestoinyou
BringbestoinyouBringbestoinyou
Bringbestoinyou
 
Windows azure mobile service
Windows azure mobile serviceWindows azure mobile service
Windows azure mobile service
 
Test studiowebinaraugcodedstep
Test studiowebinaraugcodedstepTest studiowebinaraugcodedstep
Test studiowebinaraugcodedstep
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript Functions and Objects in JavaScript
Functions and Objects in JavaScript
 
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
Create Hybrid Mobile Application with Icenium and Kendo UI Mobile
 
Cloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using EverliveCloud Based Enterprise Apps using Everlive
Cloud Based Enterprise Apps using Everlive
 
A Look into Automated Web UI Test
A Look into Automated Web UI TestA Look into Automated Web UI Test
A Look into Automated Web UI Test
 
Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI Windows phone 8 app using Kendo UI
Windows phone 8 app using Kendo UI
 
Cross platformmobileapp
Cross platformmobileappCross platformmobileapp
Cross platformmobileapp
 
Windows aazuremobileservices
Windows aazuremobileservicesWindows aazuremobileservices
Windows aazuremobileservices
 
Rad controlforwindows25thapril
Rad controlforwindows25thaprilRad controlforwindows25thapril
Rad controlforwindows25thapril
 
Data asservice
Data asserviceData asservice
Data asservice
 
WCF for begineers
WCF  for begineersWCF  for begineers
WCF for begineers
 
Windows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarchWindows storemindcrcaker23rdmarch
Windows storemindcrcaker23rdmarch
 

Kürzlich hochgeladen

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 

Kürzlich hochgeladen (20)

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Java script

  • 1. Functions in JavaScript Dhananjay Kumar [@debug_mode] Delhi Chapter Lead Microsoft MVP Mindcracker MVP Telerik Evangelist http://debugmode.net FB: Dhananjay.25july@gmail.com
  • 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