Functions

Function Declaration in JavaScript

A function (method) is a block of code that can be called from another location in the program or class.


Syntax
function functionName(functionParameters) {
    //code to execute
    return value; //optional return statement
}

functionName(passedParameters); //calling a function

Notes

functionName is defined by the programmer.

functionParameters contain data used within the method, stored using variables. Parameters are defined as: (dataType parameterName), which is the data type of the input parameter and the name of the parameter as seen within the function. There can be multiple parameters separated by a comma.

The function can only access other global functions and variables, ones passed into the function, as well as ones within the same class.


Example
function findMax(a, b) {
    return a > b ? a : b;
}

maximum = findMax(3, 5); //will calculate max and assign 5

See Also
Documentation

Functions - JavaScript | MDN

Add to Slack

< Try Catch Block   |   Anonymous Functions >

© 2019 SyntaxDB. All Rights Reserved.