Function Declaration in JavaScript

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

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

functionName(passedParameters); //calling a function


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.

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

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

See Also

Functions - JavaScript | MDN

Add to Slack

< Try Catch Block   |   Anonymous Functions >

© 2019 SyntaxDB. All Rights Reserved.