0% found this document useful (0 votes)
9 views13 pages

02_Javascript_Function

JavaScript functions are blocks of code that perform specific tasks and are executed when invoked. They can take parameters and return values, with function expressions allowing them to be stored in variables. Arrow functions provide a concise syntax for function expressions, eliminating the need for certain keywords and curly brackets.

Uploaded by

victor thomas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

02_Javascript_Function

JavaScript functions are blocks of code that perform specific tasks and are executed when invoked. They can take parameters and return values, with function expressions allowing them to be stored in variables. Arrow functions provide a concise syntax for function expressions, eliminating the need for certain keywords and curly brackets.

Uploaded by

victor thomas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

JAVASCRIPT

Function
JavaScript Functions
• A JavaScript function is a block of code designed to perform a particular task.
• A JavaScript function is executed when "something" invokes it (calls it).

• 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.

2
Function Return
• When JavaScript reaches a return statement, the function will stop
executing.
• Functions often compute a return value. The return value is "returned"
back to the "caller":

3
Function Expressions
• A function expression can be stored in a variable:

• The function is actually an anonymous function (a function


without a name).
• Functions stored in variables do not need function names. They
are always invoked (called) using the variable name.

4
The Function() Constructor
• Functions can also be defined with a built-in JavaScript function
constructor called Function().

• You actually don't have to use the function constructor. The example
above is the same as writing:

5
Invoking a Function as a Function

6
Invoking a Function as a Method
• In JavaScript you can define functions as object methods.

• The following example creates an object (myObject), with two


properties (firstName and lastName), and a method (fullName):

7
Invoking a Function with a Function Constructor
• If a function invocation is preceded with the new keyword, it is a
constructor invocation.

8
“this” keyword
• In JavaScript, “this” keyword refers to an object.
• Which object depends on how this is being invoked (used or called).
• “this” keyword refers to different objects depending on how it is used.

9
Convert Fahrenheit to Celsius:

Functions Used as Variable Values

10
Arrow Function
• Arrow functions allows a short syntax for writing function expressions.
• The function keyword, the return keyword, and the curly brackets are not
needed.
• Arrow functions are not hoisted (must be defined before they are used).
• Using const is safer than using var, because a function expression is
always constant value.
• Don’t omit the return keyword and the curly brackets if the function is a
single statement.

11
Arrow Function
<script>
let myFunction = (a, b) => a * b;
<script> document.getElementById("demo").innerHTML = myFunction(4, 5);
Function myFunction(a,b){ </script>
return a*b;
}
document.getElementById("demo").innerHTML = myFunction(4, 5);
</script>

<script> <script>
let hello = ""; let hello = "";
hello = () => {
hello = function() { return "Hello World!";
return "Hello World!"; }
} document.getElementById("demo").innerHTML = hello();
document.getElementById("demo").innerHTML = hello(); </script>
</script>
12
Let's Try JS Function.

13

You might also like