C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: We introduce multiplyBy2, a function that receives an argument and returns that value multiplied by 2.
JavaScript program that uses simple function
function multiplyBy2(value) {
"use strict";
// Multiply the argument by 2.
return value * 2;
}
// Call the function.
console.log("FUNCTION RESULT: " + multiplyBy2(4));
Output
FUNCTION RESULT: 8
And: A function that prints the element to the console is called on each iteration.
JavaScript program that uses function object as argument
// Print 3 integer elements with an anonymous function.
// ... Pass the function as an argument to forEach.
[10, 20, 30].forEach(function(x) {
"use strict";
console.log("VALUE IS: " + x);
});
Output
VALUE IS: 10
VALUE IS: 20
VALUE IS: 30
Tip: We have code that is as easy to write as top-level code, but it is in a function. The "animal" variable goes out of scope.
JavaScript program that uses IIFE
(function() {
var animal = "cat";
document.getElementById("animals").textContent = animal;
})();
JavaScript program that uses return keyword
function getWord(n) {
// Return string representation of number.
if (n === 1) {
return "one";
}
else if (n === 2) {
return "two";
}
else {
return "unknown";
}
}
console.log("GETWORD: " + getWord(1));
console.log("GETWORD: " + getWord(2));
console.log("GETWORD: " + getWord(1000));
Output
GETWORD: one
GETWORD: two
GETWORD: unknown
JavaScript program that uses return
function getWord(n) {
if (n === 1) {
return "one";
}
}
// If no return statement is reached, we get undefined.
console.log("RESULT: " + getWord(1000));
Output
RESULT: undefined
Left: The left side of the arrow function is a list of arguments—this is the same as a "function" in JavaScript.
Right: This is the return value of the function. The value is computed and returned (no "return" keyword is used).
JavaScript program that uses arrow function, var
// Use an arrow function and store the function in a variable.
var pets = (cats, dogs) => (cats + dogs)
// Call arrow function.
var result = "I have " + pets(1, 2) + " pets.";
console.log("ARROW FUNCTION: " + result);
Output
ARROW FUNCTION: I have 3 pets.
Version 1: We call a function expression that returns a value. We time many iterations of this logic.
Version 2: We inline the result from the function expression instead of wrapping it in a function block.
Result: There is significant cost to using a function expression, even if it should be easily inlined.
JavaScript program that benchmarks IIFE syntax
var x1 = performance.now();
var test = 0;
// Version 1: call function expression.
for (var i = 0; i < 100000; i++) {
test += (function() {
return 1;
})();
}
var x2 = performance.now();
// Version 2: inlined value.
for (var i = 0; i < 100000; i++) {
test += 1;
}
var x3 = performance.now();
// Results.
console.log("TIME 1: " + (x2 - x1));
console.log("TIME 2: " + (x3 - x2));
Output
TIME 1 (2019): 3.710
TIME 2: 1.979
Info: One optimization for JavaScript is to reduce the count of functions—combine them or eliminate them unless needed.
JavaScript program that uses separate, merged functions
function single1() {
console.log("RESULT 1");
}
function single2() {
console.log("RESULT 2");
}
function combo(x) {
if (x === 0) {
console.log("RESULT 3");
}
else {
console.log("RESULT 4");
}
}
// Call functions.
single1();
single2();
combo(0);
combo(1);
Output
RESULT 1
RESULT 2
RESULT 3
RESULT 4
Quote: With Ignition, V8 compiles JavaScript functions to a concise bytecode, which is between 50% to 25% the size of the equivalent baseline machine code. This bytecode is then executed by a high-performance interpreter....
V8 JavaScript Engine: v8.dev