.
.
.
alert(message), prompt(message, default) confirm(question). .
Function Declaration
function declaration.
:
function showMessage() {
alert(" ");
}
function parameters ( ) the function body .
function name(parameters) {
...body...
}
: showMessage().
For instance:
function showMessage() {
alert( ' ' );
}
showMessage();
showMessage();
showMessage() .
.
.
:
function showMessage() {
let message = "Hello, I'm JavaScript!"; // local variable
alert( message );
}
showMessage(); // Hello, I'm JavaScript!
alert( message ); // <-- !
:
let userName = 'John';
function showMessage() {
let message = 'Hello, ' + userName;
alert(message);
}
showMessage(); // Hello, John
.
:
let userName = 'John';
function showMessage() {
userName = "Bob"; // (1)
let message = 'Hello, ' + userName;
alert(message);
}
alert( userName ); // John
showMessage();
alert( userName ); // Bob
.
userName :
let userName = 'John';
function showMessage() {
let userName = "Bob"; //
let message = 'Hello, ' + userName; // Bob
alert(message);
}
// userName
showMessage();
alert( userName ); // John, ,
userName global.
( ).
. . .
Parameters
parameters ( function arguments) .
: from text.
function showMessage(from, text) { // arguments: from, text
alert(from + ': ' + text);
}
showMessage('Ann', 'Hello!'); // Ann: Hello! (*)
showMessage('Ann', "What's up?"); // Ann: What's up? (**)
(*) (**) from text. .
from . from :
function showMessage(from, text) {
from = '*' + from + '*'; // make "from" look nicer
alert( from + ': ' + text );
}
let from = "Ann";
showMessage(from, "Hello"); // *Ann*: Hello
// "from"
alert( from ); // Ann
Parameter undefined.
showMessage(from, text) :
showMessage("Ann");
"Ann: undefined". text text === undefined.
text =:
function showMessage(from, text = "no text given") {
alert( from + ": " + text );
}
showMessage("Ann"); // Ann: no text given
text "no text given"
"no text given" :
function showMessage(from, text = anotherFunction()) {
// anotherFunction()
// text
}
.
anotherFunction() showMessage() text.
.
undefined:
function showMessage(text) {
if (text === undefined) {
text = 'empty message';
}
alert(text);
}
showMessage(); // empty message
||:
// text "" 'empty'
function showMessage(text) {
text = text || 'empty';
...
}
nullish coalescing operator ?? falsy values 0:
// "count" "unknown"
function showCount(count) {
alert(count ?? "unknown");
}
showCount(0); // 0
showCount(null); // unknown
showCount(); // unknown
.
:
function sum(a, b) {
return a + b;
}
let result = sum(1, 2);
alert( result ); // 3
return ( result ).
return :
function checkAge(age) {
if (age >= 18) {
return true;
} else {
return confirm('Do you have permission from your parents?');
}
}
let age = prompt('How old are you?', 18);
if ( checkAge(age) ) {
alert( 'Access granted' );
} else {
alert( 'Access denied' );
}
return .
:
function showMovie(age) {
if ( !checkAge(age) ) {
return;
}
alert( "Showing you the movie" ); // (*)
// ...
}
checkAge(age) false showMovie alert.
``smart header= return undefined
undefined:
function doNothing() {
/* empty */
}
alert(doNothing() === undefined); // true
return return undefined:
function doNothing() {
return;
}
alert(doNothing() === undefined); // true
````warn header=" `return` "
`return` :
```js
return
(some + long + expression + or + whatever * f(a) + f(b))
```
`return`. :
```js
return;
(some + long + expression + or + whatever * f(a) + f(b))
```
return .
`return`. :
```js
return (
some + long + expression
+ or +
whatever * f(a) + f(b)
)
```
.
. . .
.
"show" .
Function starting with
"get","calc","create","check".
:
showMessage(..) // shows a message
getAge(..) // returns the age (gets it somehow)
calcSum(..) // calculates a sum and returns the result
createForm(..) // creates a form (and usually returns it)
checkPermission(..) // checks a permission, returns true/false
.
.
( ).
:
getAgealert( ).createForm( ).checkPermissionaccess granted/deniedmessage ( ).
. .
==
. . .
!
:
function showPrimes(n) {
nextPrime: for (let i = 2; i < n; i++) {
for (let j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
}
alert(i); // a prime
}
}
isPrime(n) :
function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
alert(i); // a prime
}
}
function isPrime(n) {
for (let i = 2; i < n; i++) {
if ( n % i == 0) return false;
}
return true;
}
(isPrime). self-describing.
.
:
function name(parameters, delimited, by, comma) {
/* code */
}
- .
- .
-
undefined.
.
.
:
- .
- .
-
create,show,get,check.
. . .
<code><pre>10 (plnkr, JSBin, codepen)