SlideShare a Scribd company logo
1 of 17
PREDEFINED & USER
DEFINED FUNCTIONS
Presenting By – Swapnil Yadav ,
Class - 10
The Function Definition
• Control is passed to the function by the function
call. The statements within the function body will
then be executed.
function PrintMessage()
{
alert("A message for you:nnHave a nice day!");
}
• After the statements in the function have
completed, control is passed back to the place
where the function was called.
General Function Definition Syntax
function FunctionName ( parameter1, . . . , parametern )
{
variable declaration(s)
statement(s)
}
• If there are no parameters, there should be nothing inside of
the ()'s
function FunctionName()
{
...
}
• There may be no variable declarations.
Functions
• When program control encounters a function name, the function is called
(invoked).
• Program control passes to the function.
• The function is executed.
• Control is passed back to the place where the function
was called.
• A function is a block of code that will be executed
when "someone" calls it. In JavaScript, we can define
our own functions, called user-defined functions, or
we can use built-in functions already defined in the
JavaScript language.
The Function Call
• Passes program control to the function
• Must match the definition in name and number of
arguments
........
function PrintMessage()
{
alert("A message for you:nnHave a nice day!");
}
........
<body>
<script type="text/javascript">
<!--
PrintMessage();
//-->
</script>
</body>
Same name and
no arguments
(nothing inside of
the parentheses)
Sample Function Call
alert is the name of a predefined
function in the JavaScript language
alert("Hello World!"); this statement is
is known as a
function call
this is a string we are passing
as an argument (parameter) to
the alert function
Predefined Functions
• We have used several predefined functions so far:
• alert()
• prompt()
• document.write()
• toFixed()
• parseInt()
• parseFloat()
• Programmers can write their own functions.
• Typically, each module in a program’s design hierarchy chart is
implemented as a function.
• write() is a method of the document object that writes the content
"Hello World" on the web page. There are several Javascript built-in
objects such as,
• Number
• String
• RegExp
• Array
• Math
• Date
• Boolean
• Each of the above objects hold several built-in functions to perform
object related functionality. Apart from these methods, Javascript
provides few predefined functions which do not stick to a particular
object type but are global.
Predefined Function - String
• String() function converts the object argument passed to it to a string
value.
•
• var obj1=new Boolean(0);
• var obj2=new Boolean(1);
• var obj3=new Date();
•
• document.write(String(obj1));
• document.write(String(obj2));
• document.write(String(obj3));
• Result:
• false
• true
• Thu Jul 19 2012 23:28:08 GMT+0530 (India Standard Time
Predefined Function - parseInt
• parseInt()
• parseInt() function takes string as a parameter and converts it to
integer.
• document.write(parseInt("50"));
• document.write(parseInt("77 days"));
• document.write(parseInt("this is 7"));
• Result:
• 50
• 77
• NaN
• An optional radix parameter can also be used to specify the number
system to be used to parse the string argument. For example,
• document.write(parseInt("10",16));
• Result:
16
Predefined Function - parseFloat
• parseFloat()
• parseFloat() function takes a string as parameter and parses it to a
floating point number.
• document.write(parseFloat("10.33"));
• document.write(parseFloat("15 66 75"));
• document.write(parseFloat("this is 77"));
• document.write(pareFloat(" 77 "));
• Result:
• 10.33
• 15
• NaN
• 77
• Note: This function allows leading and trailing spaces. If the first character
in the string is not a number, then it returns NaN. If the string has more
than one set of number separated by delimiters such as spaces,
semicolons,commas then it returns only the first set of number before the
first delimiter.
User-defined Functions
• JavaScript’s predefined functions represent a collection of useful, general-purpose abstractions
• the programmer can add additional abstractions via user-defined functions
• once defined, a user-defined function can be used the same way as a predefined function
• e.g., consider converting a temperature from Fahrenheit to Celsius
tempInCelsius = (5/9) * (tempInFahr - 32);
function FahrToCelsius(tempInFahr)
// Assumes: tempInFahr is a temperature in Fahrenheit
// Returns: the equivalent temperature in Celsius
{
return (5/9) * (tempInFahr - 32);
}
this expression & assignment could be used whenever we want to convert
 requires remembering the formula every time
instead, we could define a function to encapsulate the calculation
could then call that function whenever a conversion was needed
freezing = FahrToCelsius(32); current = FahrToCelsius(78);
<head>
<title>Function Example</title>
<script type="text/javascript">
<!--
function PrintMessage()
{
alert("A message for you:nnHave a nice day!");
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
PrintMessage();
//-->
</script>
</body>
Screenshot of Function Example
User – Defined function
• Multiple JavaScript functions can be defined in the HEAD section of a HTML document. <html>
• <head> <script type="text/javascript">
• function function1( ) {
• some code
• }
• function function2( )
• {
• some code
• } </script></head>
• <body>
• …
• </body> </html>
User – Defined function
• In the following example, we define a function named welcome( ) that
takes in one parameter, named user. When the function is called, we
pass the argument "John Smith" to the function.
• <script> function welcome( user )
• {
• alert( "Welcome " + user + " to my website!" );
• }
• </script> <script>
• welcome( "John Smith" );
• </script>
predefined and user defined functions

More Related Content

What's hot (20)

Introduction to Java -unit-1
Introduction to Java -unit-1Introduction to Java -unit-1
Introduction to Java -unit-1
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
php
phpphp
php
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 
Files in php
Files in phpFiles in php
Files in php
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php forms
Php formsPhp forms
Php forms
 
Applets
AppletsApplets
Applets
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
C programs
C programsC programs
C programs
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
C functions
C functionsC functions
C functions
 
JDBC Architecture and Drivers
JDBC Architecture and DriversJDBC Architecture and Drivers
JDBC Architecture and Drivers
 

Viewers also liked

Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Vidyasagar Mundroy
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQueryRemy Sharp
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in CPrabhu Govind
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 

Viewers also liked (6)

Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQuery
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 

Similar to predefined and user defined functions

Similar to predefined and user defined functions (20)

Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Unit 8
Unit 8Unit 8
Unit 8
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
Basic c++
Basic c++Basic c++
Basic c++
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Functions
FunctionsFunctions
Functions
 
Function
Function Function
Function
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Functions
FunctionsFunctions
Functions
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Function
FunctionFunction
Function
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Functions
FunctionsFunctions
Functions
 

Recently uploaded

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 

Recently uploaded (20)

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

predefined and user defined functions

  • 1. PREDEFINED & USER DEFINED FUNCTIONS Presenting By – Swapnil Yadav , Class - 10
  • 2. The Function Definition • Control is passed to the function by the function call. The statements within the function body will then be executed. function PrintMessage() { alert("A message for you:nnHave a nice day!"); } • After the statements in the function have completed, control is passed back to the place where the function was called.
  • 3. General Function Definition Syntax function FunctionName ( parameter1, . . . , parametern ) { variable declaration(s) statement(s) } • If there are no parameters, there should be nothing inside of the ()'s function FunctionName() { ... } • There may be no variable declarations.
  • 4. Functions • When program control encounters a function name, the function is called (invoked). • Program control passes to the function. • The function is executed. • Control is passed back to the place where the function was called. • A function is a block of code that will be executed when "someone" calls it. In JavaScript, we can define our own functions, called user-defined functions, or we can use built-in functions already defined in the JavaScript language.
  • 5. The Function Call • Passes program control to the function • Must match the definition in name and number of arguments ........ function PrintMessage() { alert("A message for you:nnHave a nice day!"); } ........ <body> <script type="text/javascript"> <!-- PrintMessage(); //--> </script> </body> Same name and no arguments (nothing inside of the parentheses)
  • 6. Sample Function Call alert is the name of a predefined function in the JavaScript language alert("Hello World!"); this statement is is known as a function call this is a string we are passing as an argument (parameter) to the alert function
  • 7. Predefined Functions • We have used several predefined functions so far: • alert() • prompt() • document.write() • toFixed() • parseInt() • parseFloat() • Programmers can write their own functions. • Typically, each module in a program’s design hierarchy chart is implemented as a function.
  • 8. • write() is a method of the document object that writes the content "Hello World" on the web page. There are several Javascript built-in objects such as, • Number • String • RegExp • Array • Math • Date • Boolean • Each of the above objects hold several built-in functions to perform object related functionality. Apart from these methods, Javascript provides few predefined functions which do not stick to a particular object type but are global.
  • 9. Predefined Function - String • String() function converts the object argument passed to it to a string value. • • var obj1=new Boolean(0); • var obj2=new Boolean(1); • var obj3=new Date(); • • document.write(String(obj1)); • document.write(String(obj2)); • document.write(String(obj3)); • Result: • false • true • Thu Jul 19 2012 23:28:08 GMT+0530 (India Standard Time
  • 10. Predefined Function - parseInt • parseInt() • parseInt() function takes string as a parameter and converts it to integer. • document.write(parseInt("50")); • document.write(parseInt("77 days")); • document.write(parseInt("this is 7")); • Result: • 50 • 77 • NaN • An optional radix parameter can also be used to specify the number system to be used to parse the string argument. For example, • document.write(parseInt("10",16)); • Result: 16
  • 11. Predefined Function - parseFloat • parseFloat() • parseFloat() function takes a string as parameter and parses it to a floating point number. • document.write(parseFloat("10.33")); • document.write(parseFloat("15 66 75")); • document.write(parseFloat("this is 77")); • document.write(pareFloat(" 77 ")); • Result: • 10.33 • 15 • NaN • 77 • Note: This function allows leading and trailing spaces. If the first character in the string is not a number, then it returns NaN. If the string has more than one set of number separated by delimiters such as spaces, semicolons,commas then it returns only the first set of number before the first delimiter.
  • 12. User-defined Functions • JavaScript’s predefined functions represent a collection of useful, general-purpose abstractions • the programmer can add additional abstractions via user-defined functions • once defined, a user-defined function can be used the same way as a predefined function • e.g., consider converting a temperature from Fahrenheit to Celsius tempInCelsius = (5/9) * (tempInFahr - 32); function FahrToCelsius(tempInFahr) // Assumes: tempInFahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (tempInFahr - 32); } this expression & assignment could be used whenever we want to convert  requires remembering the formula every time instead, we could define a function to encapsulate the calculation could then call that function whenever a conversion was needed freezing = FahrToCelsius(32); current = FahrToCelsius(78);
  • 13. <head> <title>Function Example</title> <script type="text/javascript"> <!-- function PrintMessage() { alert("A message for you:nnHave a nice day!"); } //--> </script> </head> <body> <script type="text/javascript"> <!-- PrintMessage(); //--> </script> </body>
  • 15. User – Defined function • Multiple JavaScript functions can be defined in the HEAD section of a HTML document. <html> • <head> <script type="text/javascript"> • function function1( ) { • some code • } • function function2( ) • { • some code • } </script></head> • <body> • … • </body> </html>
  • 16. User – Defined function • In the following example, we define a function named welcome( ) that takes in one parameter, named user. When the function is called, we pass the argument "John Smith" to the function. • <script> function welcome( user ) • { • alert( "Welcome " + user + " to my website!" ); • } • </script> <script> • welcome( "John Smith" ); • </script>