0% found this document useful (0 votes)
0 views

Js Functions

Uploaded by

vikashgrg06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Js Functions

Uploaded by

vikashgrg06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

FUNCTIONS

WHAT IS A FUNCTION?
 Function is a group of statement that together perform a specific task
 We divide a code into separate functions
 Functions usually take in data, process it and return a result
 It can be used over, over and over again
ADVANTAGES OF FUNCTION
 Avoid repetition of code
 Increase program readability
 Divide complex problem into simpler segments
 Reduce chance of error
 Modifying program becomes easier
TYPES OF FUNCTIONS
 Inbuilt
 Library provide numerous inbuilt functions
 Example :
toString() converts an object into the string.
equals() checks if two objects are equal.

• Defined By User
COMPONENTS OF FUNCTION
Declaration of a function :
A JavaScript function is defined with the function keyword, followed by
a name, followed by parentheses ().
 Tells the browser:
 name of function,
 input parameters
 The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
 Syntax :
 function name(parameter1, parameter2, parameter3) {
// code to be executed
}.
Defination of a function
 Actual body

 Function Arguments and Parameters


 Function parameters are listed inside the parentheses () in the function
definition.
 Function arguments are the values received by the function when it is
invoked.
 Inside the function, the arguments (the parameters) behave as local
variables.
METHOD NAME
 Must be unique
 Must be a verb and start with a lowercase letter.
 Advisable to follow CamelCase :
sumTwoNumber ,sumThreeNumber ,subNum etc
 We can use underscore _ and $ sings
 We can not use Keywords
CALLING A FUNCTION / FUNCTION
INVOCATION
 Call statement consist of function name and required argument
enclosed in round brackets

 The code inside the function will execute when


"something" invokes (calls) the function:
 When an event occurs (when a user clicks a button)
 When it is invoked (called) from JavaScript code
 Automatically (self invoked)

 Accessing a function without () will return the function


Object(Defination) instead of the function result.

A method returns to the code that invoked it when:
 It completes all the statements in the method
 It reaches a return statement
 Throws an exception
RETURN
 When JavaScript reaches a return statement, the function will stop
executing.
 If the function was invoked from a statement, JavaScript will "return" to
execute the code after the invoking statement.
 Functions often compute a return value. The return value is "returned"
back to the "caller"
ARROW FUNCTIONS
 Arrow functions were introduced in ES6.
 Arrow functions allow us to write shorter function syntax:

 let myFunction = (a, b) => a * b;


 Example
 hello = function() {
return "Hello World!";
}
 Without function
 hello = () => {
return "Hello World!";
}
 Without return
 hello = () => "Hello World!";
 With parameter
 hello = (val) => "Hello " + val;
Thanks
?

You might also like