- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Arithmetic
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Best Practices
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Functions
JavaScript Functions
A JavaScript function is a section of code designed to accomplish a specific task.
A JavaScript function is executed when it is invoked by "something" (calls it).
return p1 * p2; // The function returns the product of p1 and p2
}
JavaScript Function Syntax
The function keyword, followed by a name, followed by parentheses, defines a JavaScript function ().
The following characters are permitted in function names: letters, numbers, underscores, and dollar signs (same rules as variables).
There may be parameter names within the parentheses, separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}
// code to be executed
}
The parameters of a function are listed within parentheses () in the function definition.
Arguments to a function are the values it receives when it is called.
The arguments (the parameters) behave as local variables within the function.
Similar to a Procedure or a Subroutine in other programming languages, a Function exists in Python.
Function Invocation And Function Return
The function's code will execute when it is invoked (called) by "something":
- When an event occurs (when a user clicks a button)
- When it is invoked (called) from JavaScript code
- Automatically (self invoked)
You will learn a lot more about function invocation later in this tutorial.
Function Return
When JavaScript reaches a return statement, execution of the function will cease.
If the function was invoked from a statement, JavaScript will "return" to execute the code following the statement that invoked the function.
Typically, functions compute a return value. The return value is "returned" to the "calling" party:
Example
return a * b; // Function returns the product of a and b
}
The result in x will be: 12
Why Functions?
You can reuse code: Define the code once and utilise it repeatedly.
It is possible to use the same code multiple times with different arguments to produce distinct outcomes.
return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius(77);
The () Operator Invokes The Function
Using the preceding example, toCelsius is the function object, while toCelsius() is the function result.
When a function is accessed without (), the function object is returned instead of the function result.
return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius;
Functions Used As Variable Values
In all types of formulas, assignments, and calculations, functions can be utilised in the same way variables are.
Example
var text = "The temperature is " + x + " Celsius";
You can use the function directly, as a variable value:
Local Variables
Declared variables within a JavaScript function are LOCAL to that function.
Local variables are only accessible within their respective functions.
function myFunction() {
var carName = "Volvo";
// code here CAN use carName
}
// code here can NOT use carName
Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.
Local variables are created when a function starts, and deleted when the function is completed.
COMPANY
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP