[ Web Proxy ]
URL:
Viewing: https://ar.javascript.info/function-basics [Back]  [Original]

:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

.

.

.

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

.

.

( ).

:

  • getAge alert ( ).
  • createForm ( ).
  • checkPermission access granted/denied message ( ).

. .

.

jQuery $. Lodash _.

.

==

. . .

!

showPrimes(n) . n.

:

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 .

. . .

true age 18.

:

function checkAge(age) {
  if (age > 18) {
    return true;
  } else {
    // ...
    return confirm('Did parents allow you?');
  }
}

else ?

function checkAge(age) {
  if (age > 18) {
    return true;
  }
  // ...
  return confirm('Did parents allow you?');
}

.

true age 18.

:

function checkAge(age) {
    if (age > 18) {
        return true;
    } else {
        return confirm("Did parents allow you?");
    }
}

if .

checkAge:

  1. ?
  2. OR ||

'?':

function checkAge(age) {
    return age > 18 ? true : confirm("Did parents allow you?");
}

Using OR || (the shortest variant):

function checkAge(age) {
    return age > 18 || confirm("Did parents allow you?");
}

age > 18 .

min(a,b) a b.

:

min(2, 5) == 2
min(3, -1) == -1
min(1, 1) == 1

if:

function min(a, b) {
    if (a < b) {
        return a;
    } else {
        return b;
    }
}

'?':

function min(a, b) {
    return a < b ? a : b;
}

a == b .

pow(x,n) x n. , x n .

pow(3, 2) = 3 * 3 = 9
pow(3, 3) = 3 * 3 * 3 = 27
pow(1, 100) = 1 * 1 * ...* 1 = 1

x n pow(x,n).

n: 1.

function pow(x, n) {
  let result = x;

  for (let i = 1; i < n; i++) {
    result *= x;
  }

  return result;
}

let x = prompt("x?", '');
let n = prompt("n?", '');

if (n < 1) {
  alert(`Power ${n} is not supported, use a positive integer`);
} else {
  alert( pow(x, n) );
}


Web Proxy Viewer  |  New URL  |  Original Page