JAVASCRIPT Tutorial
A function is a block of code that performs a specific task and returns a value.
To declare a function, use the following syntax:
function functionName(parameters) {
// Function code
}
To call a function, use the following syntax:
functionName(arguments);
A function can return a value using the return
statement.
function functionName() {
return value;
}
Calculation
Functions can perform calculations by using variables, operators, and control flow statements.
// Function to calculate the area of a triangle
function calculateArea(base, height) {
return (base * height) / 2;
}
// Call the function and store the result in a variable
const area = calculateArea(5, 3);
console.log(`The area of the triangle is: ${area}`);
In this example:
calculateArea
function is defined with two parameters, base
and height
.(base * height) / 2
.5
and 3
, and the result is stored in the area
variable.