SlideShare a Scribd company logo
2
JavaScript - Functions
 A function is a group of reusable code which can be
called anywhere in your program.
 This eliminates the need of writing the same code
again and again.
 It helps programmers in writing modular codes.
Functions allow a programmer to divide a big program
into a number of small and manageable functions.
Most read
6
Calling a Function
<html>
<head>
<script type = "text/javascript">
function sayHello() {
document.write ("Hello there!"); }
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello"> </form>
<p>Use different text in write method and then try...</p>
</body>
</html>
OUTPUT: Hello there!
Most read
8
Assignment Question
 Explain the Function in javascript with discuss the
following terms.
 Calling Function
 Function Parameter
Most read
JavaScript - Functions
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
JavaScript - Functions
 A function is a group of reusable code which can be
called anywhere in your program.
 This eliminates the need of writing the same code
again and again.
 It helps programmers in writing modular codes.
Functions allow a programmer to divide a big program
into a number of small and manageable functions.
Cont…
 The most common way to define a function in
JavaScript is by using the function keyword, followed
by a unique function name, a list of parameters (that
might be empty), and a statement block surrounded
by curly braces.
Syntax
 Syntax
 The basic syntax is shown here.
 <script type = "text/javascript"> <!-- function
functionname(parameter-list) { statements } //-->
</script>
Example
 It defines a function called sayHello that takes no
parameters −
 <script type = "text/javascript"> <!-- function
sayHello() { alert("Hello there"); } //--> </script>
Calling a Function
<html>
<head>
<script type = "text/javascript">
function sayHello() {
document.write ("Hello there!"); }
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello"> </form>
<p>Use different text in write method and then try...</p>
</body>
</html>
OUTPUT: Hello there!
Function Parameters
 <html>
 <head>
 <script type = "text/javascript">
 function sayHello(name, age) {
 document.write (name + " is " + age + " years old."); } </script>
 </head>
 <body>
 <p>Click the following button to call the function</p>
 <form>
 <input type = "button" onclick = "sayHello('Zara', 7)" value = "Say Hello">
 </form>
 <p>Use different parameters inside the function and then try...</p> </body>
 </html>
 Output: Zara is 7 years old.
Assignment Question
 Explain the Function in javascript with discuss the
following terms.
 Calling Function
 Function Parameter

More Related Content

What's hot (19)

Introduction to thymeleaf
Introduction to thymeleafIntroduction to thymeleaf
Introduction to thymeleaf
NexThoughts Technologies
 
Modular Angular JS
Modular Angular JSModular Angular JS
Modular Angular JS
Dhyego Fernando
 
Text 1
Text 1Text 1
Text 1
Vikram Sinwal
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
Asya Dudnik
 
Jsp & struts
Jsp & strutsJsp & struts
Jsp & struts
Hansi Thenuwara
 
2.routing in zend framework 3
2.routing in zend framework 32.routing in zend framework 3
2.routing in zend framework 3
Razvan Raducanu, PhD
 
Introduction of javascript
Introduction of javascriptIntroduction of javascript
Introduction of javascript
syeda zoya mehdi
 
Java script programs
Java script programsJava script programs
Java script programs
ITz_1
 
Let's write secure drupal code!
Let's write secure drupal code!Let's write secure drupal code!
Let's write secure drupal code!
Balázs Tatár
 
Java script
Java scriptJava script
Java script
Gourishankar R Pujar
 
Thymeleaf Introduction
Thymeleaf IntroductionThymeleaf Introduction
Thymeleaf Introduction
Anthony Chen
 
JavaScript Operators
JavaScript OperatorsJavaScript Operators
JavaScript Operators
Dr. Jasmine Beulah Gnanadurai
 
symfony & jQuery (phpDay)
symfony & jQuery (phpDay)symfony & jQuery (phpDay)
symfony & jQuery (phpDay)
Massimiliano Arione
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & Implementation
Soumen Santra
 
Loops
LoopsLoops
Loops
Max Friel
 
Inline
InlineInline
Inline
ekeoguob
 
Drupal sins 2016 10-06
Drupal sins 2016 10-06Drupal sins 2016 10-06
Drupal sins 2016 10-06
Aaron Crosman
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
Vineet Kumar Saini
 
Lazy Loading and Object Proxying Shenangians
Lazy Loading and Object Proxying ShenangiansLazy Loading and Object Proxying Shenangians
Lazy Loading and Object Proxying Shenangians
John Barton
 

Similar to Java script functions (20)

CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed
 
11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
Java scriptfunction
Java scriptfunctionJava scriptfunction
Java scriptfunction
Jesus Obenita Jr.
 
Lecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentationLecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentation
GomathiUdai
 
javascript function ujjwal matoliya.pptx
javascript function ujjwal matoliya.pptxjavascript function ujjwal matoliya.pptx
javascript function ujjwal matoliya.pptx
ujjwalmatoliya
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
JyothiAmpally
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
Vinod Srivastava
 
Javascript
JavascriptJavascript
Javascript
Gita Kriz
 
Javascript Basics by Bonny
Javascript Basics by BonnyJavascript Basics by Bonny
Javascript Basics by Bonny
Bonny Chacko
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
team11vgnt
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
Selvin Josy Bai Somu
 
Java script
 Java script Java script
Java script
bosybosy
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
Alaref Abushaala
 
Intro to JavaScript: FUNdamentals (3/7/18)
Intro to JavaScript: FUNdamentals (3/7/18)Intro to JavaScript: FUNdamentals (3/7/18)
Intro to JavaScript: FUNdamentals (3/7/18)
Justin Ezor
 
Week 6 java script loops
Week 6   java script loopsWeek 6   java script loops
Week 6 java script loops
brianjihoonlee
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
Charles Russell
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
Ajay Khatri
 
Java Script
Java ScriptJava Script
Java Script
Kalidass Balasubramaniam
 
Java script
Java scriptJava script
Java script
Jay Patel
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed
 
11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
Lecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentationLecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentation
GomathiUdai
 
javascript function ujjwal matoliya.pptx
javascript function ujjwal matoliya.pptxjavascript function ujjwal matoliya.pptx
javascript function ujjwal matoliya.pptx
ujjwalmatoliya
 
Javascript Basics by Bonny
Javascript Basics by BonnyJavascript Basics by Bonny
Javascript Basics by Bonny
Bonny Chacko
 
Java script
 Java script Java script
Java script
bosybosy
 
Intro to JavaScript: FUNdamentals (3/7/18)
Intro to JavaScript: FUNdamentals (3/7/18)Intro to JavaScript: FUNdamentals (3/7/18)
Intro to JavaScript: FUNdamentals (3/7/18)
Justin Ezor
 
Week 6 java script loops
Week 6   java script loopsWeek 6   java script loops
Week 6 java script loops
brianjihoonlee
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
Charles Russell
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
Ajay Khatri
 
Ad

More from chauhankapil (20)

Gray level transformation
Gray level transformationGray level transformation
Gray level transformation
chauhankapil
 
Elements of visual perception
Elements of visual perceptionElements of visual perception
Elements of visual perception
chauhankapil
 
JSP Client Request
JSP Client RequestJSP Client Request
JSP Client Request
chauhankapil
 
Jsp server response
Jsp   server responseJsp   server response
Jsp server response
chauhankapil
 
Markov decision process
Markov decision processMarkov decision process
Markov decision process
chauhankapil
 
RNN basics in deep learning
RNN basics in deep learningRNN basics in deep learning
RNN basics in deep learning
chauhankapil
 
Introduction to generative adversarial networks (GANs)
Introduction to generative adversarial networks (GANs)Introduction to generative adversarial networks (GANs)
Introduction to generative adversarial networks (GANs)
chauhankapil
 
Bayesian probabilistic interference
Bayesian probabilistic interferenceBayesian probabilistic interference
Bayesian probabilistic interference
chauhankapil
 
Jsp
JspJsp
Jsp
chauhankapil
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
chauhankapil
 
Knowledge acquistion
Knowledge acquistionKnowledge acquistion
Knowledge acquistion
chauhankapil
 
Knowledge based system
Knowledge based systemKnowledge based system
Knowledge based system
chauhankapil
 
Introduction of predicate logics
Introduction of predicate  logicsIntroduction of predicate  logics
Introduction of predicate logics
chauhankapil
 
Types of inheritance in java
Types of inheritance in javaTypes of inheritance in java
Types of inheritance in java
chauhankapil
 
Representation of syntax, semantics and Predicate logics
Representation of syntax, semantics and Predicate logicsRepresentation of syntax, semantics and Predicate logics
Representation of syntax, semantics and Predicate logics
chauhankapil
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
chauhankapil
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
chauhankapil
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
chauhankapil
 
Methods in java
Methods in javaMethods in java
Methods in java
chauhankapil
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
chauhankapil
 
Gray level transformation
Gray level transformationGray level transformation
Gray level transformation
chauhankapil
 
Elements of visual perception
Elements of visual perceptionElements of visual perception
Elements of visual perception
chauhankapil
 
JSP Client Request
JSP Client RequestJSP Client Request
JSP Client Request
chauhankapil
 
Jsp server response
Jsp   server responseJsp   server response
Jsp server response
chauhankapil
 
Markov decision process
Markov decision processMarkov decision process
Markov decision process
chauhankapil
 
RNN basics in deep learning
RNN basics in deep learningRNN basics in deep learning
RNN basics in deep learning
chauhankapil
 
Introduction to generative adversarial networks (GANs)
Introduction to generative adversarial networks (GANs)Introduction to generative adversarial networks (GANs)
Introduction to generative adversarial networks (GANs)
chauhankapil
 
Bayesian probabilistic interference
Bayesian probabilistic interferenceBayesian probabilistic interference
Bayesian probabilistic interference
chauhankapil
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
chauhankapil
 
Knowledge acquistion
Knowledge acquistionKnowledge acquistion
Knowledge acquistion
chauhankapil
 
Knowledge based system
Knowledge based systemKnowledge based system
Knowledge based system
chauhankapil
 
Introduction of predicate logics
Introduction of predicate  logicsIntroduction of predicate  logics
Introduction of predicate logics
chauhankapil
 
Types of inheritance in java
Types of inheritance in javaTypes of inheritance in java
Types of inheritance in java
chauhankapil
 
Representation of syntax, semantics and Predicate logics
Representation of syntax, semantics and Predicate logicsRepresentation of syntax, semantics and Predicate logics
Representation of syntax, semantics and Predicate logics
chauhankapil
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
chauhankapil
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
chauhankapil
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
chauhankapil
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
chauhankapil
 
Ad

Recently uploaded (20)

Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Presentación Tomografía Axial Computarizada
Presentación Tomografía Axial ComputarizadaPresentación Tomografía Axial Computarizada
Presentación Tomografía Axial Computarizada
Juliana Ovalle Jiménez
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
Journal of Soft Computing in Civil Engineering
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
May 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information TechnologyMay 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete ColumnsAxial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Journal of Soft Computing in Civil Engineering
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Presentación Tomografía Axial Computarizada
Presentación Tomografía Axial ComputarizadaPresentación Tomografía Axial Computarizada
Presentación Tomografía Axial Computarizada
Juliana Ovalle Jiménez
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
May 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information TechnologyMay 2025: Top 10 Read Articles Advanced Information Technology
May 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 

Java script functions

  • 1. JavaScript - Functions Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2. JavaScript - Functions  A function is a group of reusable code which can be called anywhere in your program.  This eliminates the need of writing the same code again and again.  It helps programmers in writing modular codes. Functions allow a programmer to divide a big program into a number of small and manageable functions.
  • 3. Cont…  The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.
  • 4. Syntax  Syntax  The basic syntax is shown here.  <script type = "text/javascript"> <!-- function functionname(parameter-list) { statements } //--> </script>
  • 5. Example  It defines a function called sayHello that takes no parameters −  <script type = "text/javascript"> <!-- function sayHello() { alert("Hello there"); } //--> </script>
  • 6. Calling a Function <html> <head> <script type = "text/javascript"> function sayHello() { document.write ("Hello there!"); } </script> </head> <body> <p>Click the following button to call the function</p> <form> <input type = "button" onclick = "sayHello()" value = "Say Hello"> </form> <p>Use different text in write method and then try...</p> </body> </html> OUTPUT: Hello there!
  • 7. Function Parameters  <html>  <head>  <script type = "text/javascript">  function sayHello(name, age) {  document.write (name + " is " + age + " years old."); } </script>  </head>  <body>  <p>Click the following button to call the function</p>  <form>  <input type = "button" onclick = "sayHello('Zara', 7)" value = "Say Hello">  </form>  <p>Use different parameters inside the function and then try...</p> </body>  </html>  Output: Zara is 7 years old.
  • 8. Assignment Question  Explain the Function in javascript with discuss the following terms.  Calling Function  Function Parameter