function showMessage(from, text) { // parameters: from, text
alert(from + ': ' + text);
}
showMessage('Ann', 'Hello!'); // Ann: Hello! (*)
showMessage('Ann', "What's up?"); // Ann: What's up? (**)
function showMessage(from, text) {
from = '*' + from + '*'; // make "from" look nicer
alert( from + ': ' + text );
}
let from = "Ann";
showMessage(from, "Hello"); // *Ann*: Hello
// the value of "from" is the same, the function modified a local copy
alert( from ); // Ann
- Javascript pass by reference and value
function showMessage(from, text = "no text given") {
alert( from + ": " + text );
}
showMessage("Ann");
showMessage("Ann", null);
showMessage("Ann", undefined);
function sum(a, b) {
return a + b;
}
let result = sum(1, 2);
alert( result ); // 3
- A name should clearly describe what the function does. When we see a function call in the code, a good name instantly gives us an understanding what it does and returns.
- A function is an action, so function names are usually verbal.
- There exist many well-known function prefixes like create…, show…, get…, check… and so on. Use them to hint what a function does.
function callable(a, f) {
if (a == null) {
f()
}
}
callable(null, function () {
alert("yahoooo")
})