JavaScript Functions
JavaScript Functions
Mobile Application
JavaScript
Functions
Development
JS Functions
• Block of code designed to perform a particular task.
function myFunction(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2
}
function multiply(a = 0, b = 1) {
return a * b;
}
multiply(); // 0
multiply(5); // 5
multiply(5, 5); // 25
JS Functions – Arguments Object
• The arguments of a function are maintained in an array-like object
• All arguments can be retrieved using arguments object
function myConcat(separator) {
var result = ''; // initialize list
// iterate through arguments
for (let i = 1; i < arguments.length; i++)
result += arguments[i] + separator;
return result;
}
// returns "red, orange, blue, "
var result = myConcat(', ', 'red', 'orange', 'blue');
// returns "elephant; giraffe; lion; cheetah; "
var result = myConcat('; ', 'elephant', 'giraffe', 'lion', 'cheetah');
// returns "sage. basil. oregano. pepper. parsley. "
var result = myConcat('. ', 'sage', 'basil', 'oregano', 'pepper', 'parsley');
Note: The arguments variable is "array-like", but not an array. It is array-like in that it has a
numbered index and a length property. However, it does not possess all of the array-
manipulation methods.
JS Functions – Rest Parameters
• The rest parameter syntax allows us to represent an indefinite number of
arguments as an array.
• Rest Parameters are received as an Array and all Array operations can be
applied
function fun(arg1, ...args) {
return args.length + 1;
}
console.log(fun(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) + " arguments passed");
// 10 arguments passed
function sumNums(...nums) {
let sum = 0;
for (let i = 0; i < nums.length; i++)
sum += nums[i];
return sum;
}
console.log(sumNums(1, 1, 1, 1, 1));
JS Functions – Pass by Value
• JavaScript is Pass by Value for Primitive Data (Number, String, Boolean)
function sum(n1, n2) {
n1 = n1 + n2;
return n1;
}
var a = 10, b = 10
console.log(a, "+", b, "=", sum(a, b))
function strFun(p1) {
p1 = "Eleven"
}
var a = "Ten"
strFun(a)
console.log(a)
JS Functions – Pass by Reference
• Objects are of Reference Type
• Therefore, JavaScript is Pass by Reference for Objects
https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference
JS Functions – Function Expression
• While the function declaration above is syntactically a statement, functions
can also be created by a function expression
• Such a function can be anonymous; it does not have to have a name. For
example, the function square could have been defined as:
const square = function(number) { return number * number }
var x = square(4) // x gets the value 16
• It gets shorter! If the function has only one statement, and the statement
returns a value
• Arrow Functions Return Value by Default
hello = () => "Hello World!";
console.log(hello())
JS Arrow Function with Parameters
• If you have parameters, you pass them inside the parentheses
hello = (val) => "Hi " + val;
console.log(hello(5))
• If you have only one parameter, you can skip the parentheses as well
hello = val => "Hi " + val;
console.log(hello(5))
What is Expected Output?