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

(Functions)
:
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

(Functions)

.

.

. .

alert(message) prompt(message, default) confirm(question) . .

(Function Declaration)

.(function declaration)

:

function showMessage() {
  alert( 'Hello everyone!' );
}

function ( ) .

function name(parameter1, parameter2, ... parameterN) {
 // body
}

: showMessage().

:

function showMessage() {
  alert( 'Hello everyone!' );
}

showMessage();
showMessage();

showMessage() . .

: .

: .

(Local variables)

.

:

function showMessage() {
  let message = "Hello, I'm JavaScript!"; // local variable

  alert( message );
}

showMessage(); // Hello, I'm JavaScript!

alert( message ); // <-- Error! The variable is local to the function

(Outer variables)

:

let userName = 'John';

function showMessage() {
  let message = 'Hello, ' + userName;
  alert(message);
}

showMessage(); // Hello, John

. .

:

let userName = 'John';

function showMessage() {
  userName = "Bob"; // (1) changed the outer variable

  let message = 'Hello, ' + userName;
  alert(message);
}

alert( userName ); // John before the function call

showMessage();

alert( userName ); // Bob, the value was modified by the function

.

. userName :

let userName = 'John';

function showMessage() {
  let userName = "Bob"; //    

  let message = 'Hello, ' + userName; // Bob
  alert(message);
}

// the function will create and use its own userName
showMessage();

alert( userName ); // John, unchanged, the function did not access the outer variable
(Global Variables)

userName .

( ).

. - . . .

.

: from text.

function showMessage(from, text) { // parameters: 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

// the value of "from" is the same, the function modified a local copy
alert( from ); // Ann

.

:

  • ( ).
  • ( ).

.

: sayMessage : from Hello.

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" .

undefined :

showMessage("Ann", undefined); // Ann: no text given

"no text given" . :

function showMessage(from, text = anotherFunction()) {
  // anotherFunction() only executed if no text given
  // its result becomes the value of text
}

.

anotherFunction() showMessage() text .

text .

. .

.

undefined:

function showMessage(from, text) {
  if (text === undefined) {
    text = '  ';
  }

  alert( from + ": " + text );
}

||:

function showMessage(from, text) {
  //       falsy   text  
  //       text == ""  
  text = text || '  ';
  ...
}

.

undefined :

function showMessage(text) {
  // ...

  if (text === undefined) { // if the parameter is missing
    text = 'empty message';
  }

  alert(text);
}

showMessage(); // empty message

|| :

function showMessage(text) {
  // if text is undefined or otherwise falsy, set it to 'empty'
  text = text || 'empty';
  ...
}

nullish coalescing ?? falsy 0 :

function showCount(count) {
  // if count is undefined or null, show "unknown"
  alert(count ?? "unknown");
}

showCount(0); // 0
showCount(null); // unknown
showCount(); // unknown

(Returning a value)

.

:

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 .

return undefined .

undefined :

function doNothing() { /* empty */ }

alert( doNothing() === undefined ); // true

return return undefined :

function doNothing() {
  return;
}

alert( doNothing() === undefined ); // true
return

return :

return
 (some + long + expression + or + whatever * f(a) + f(b))

return ; . :

return;
 (some + long + expression + or + whatever * f(a) + f(b))

.

return . ( ) :

return (
  some + long + expression
  + or +
  whatever * f(a) + f(b)
  )

.

. . .

. .

"show" .

  • "get"
  • "calc"
  • "create"
  • "check" boolean .

:

showMessage(..)     //    .
getAge(..)          //          
calcSum(..)         //      
createForm(..)      //        
checkPermission(..) //           

.

.

( )

:

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

. . . . .

.

jQuery $ . Lodash _ .

. .

Functions == Comments

. . .

Debug .

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) . - .

. .

:

function name(parameters, delimited, by, comma) {
  /* code */
}
  • .
  • . . .
  • . undefined .

.

.

:

  • . .
  • .
  • create show get check... . .

. . . .

: 4

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?');
}

.

return confirm('Did parents allow you?') if falsy .

: 4

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?');
}

OR || ( )

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

age > 18 . .

: 1

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 .

: 4

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 prompt 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