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

:
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

.

, , , -.

, . .

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

.

:

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

function, , ( ) , , , , .

function () {
  ......
}

: showMessage().

:

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

showMessage();
showMessage();

showMessage() . .

: .

: , .

, , .

:

function showMessage() {
  let message = ",  JavaScript!"; //  

  alert( message );
}

showMessage(); // ,  JavaScript!

alert( message ); // <--  , ..     

, :

let userName = '';

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

showMessage(); // , 

.

:

let userName = '';

function showMessage() {
  userName = ""; // (1)    

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

alert( userName ); //    

showMessage();

alert( userName ); // ,      

, .

, . , userName. :

let userName = '';

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

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

//          userName
showMessage();

alert( userName ); // ,  ,     

, , userName .

( ).

. . .

, .

: from text.

function showMessage(from, text) { // : from, text
  alert(from + ': ' + text);
}

showMessage('', '!'); // : ! (*)
showMessage('', " ?"); // :  ? (**)

(*) (**), from text. .

: from, . : from, . :

function showMessage(from, text) {

  from = '*' + from + '*'; //   "from"

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

let from = "";

showMessage(from, ""); // **: 

//  "from"  ,     
alert( from ); // 

, , .

:

  • , .
  • , .

, , .

, : showMessage , : from "".

, undefined.

, showMessage(from, text) :

showMessage("");

. "**: undefined". text, , text === undefined.

text , =:

function showMessage(from, text = "  ") {
  alert( from + ": " + text );
}

showMessage(""); // :   

, text , " "

" " , , . :

function showMessage(from, text = anotherFunction()) {
  // anotherFunction()      text
  //    text
}

JavaScript , .

, anotherFunction() , text.

, , text .

JavaScript

JavaScript . , .

, undefined:

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

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

||:

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

, .

, , undefined:

function showMessage(text) {
  // ...
  if (text === undefined) { //   
    text = ' ';
  }
  alert(text);
}
showMessage(); //  

||:

function showMessage(text) {
  //   text    undefined,   text  ''
  text = text || '';
  ...
}

JavaScript ??. , , , 0, .

function showCount(count) {
  //  count  undefined  null,  ""
  alert(count ?? "");
}
showCount(0); // 0
showCount(null); // 
showCount(); // 

, .

:

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

let age = prompt('  ?', 18);

if ( checkAge(age) ) {
  alert( ' ' );
} else {
  alert( ' ' );
}

return . .

:

function showMovie(age) {
  if ( !checkAge(age) ) {
    return;
  }

  alert( "  " ); // (*)
  // ...
}

, checkAge(age) false, showMovie alert.

return undefined

, , undefined:

function doNothing() { /*  */ }

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

, JavaScript return. :

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

, return.

, , , return. , , , :

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

, .

. . , , , , , .

, , , . , .

, , "show" - .

,

  • "get" ,
  • "calc" - ,
  • "create" - ,
  • "check" - , ..

:

showMessage(..)     //  
getAge(..)          //   (  - )
calcSum(..)         //     
createForm(..)      //   (   )
checkPermission(..) //  ,  true/false

, , , .

, . .

, , ( , ).

, :

  • getAge , alert ( ).
  • createForm , , ( ).
  • checkPermission , / ( ).

. , , . , , , .

, , .

, jQuery $. Lodash _.

. .

==

- . - , . , .

!

, showPrimes(n). n.

nextPrime:

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 ); // 
  }
}

isPrime(n) :

function showPrimes(n) {

  for (let i = 2; i < n; i++) {
    if (!isPrime(i)) continue;

    alert(i);  // 
  }
}

function isPrime(n) {
  for (let i = 2; i < n; i++) {
    if ( n % i == 0) return false;
  }
  return true;
}

, ? (isPrime). .

, , . .

:

function (, , ) {
  /* ,   */
}
  • .
  • . . .
  • . , undefined.

, , , .

, , , , , , .

:

  • , . , , , .
  • , .
  • , : create, show, get, check .. , , .

. JavaScript, . . .

: 4

true, age 18.

confirm :

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

- , else?

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

?

, .

: 4

true, age 18.

confirm .

function checkAge(age) {
  if (age > 18) {
    return true;
  } else {
    return confirm(' ?');
  }
}

, , if, .

checkAge:

  1. ?
  2. ||

?:

function checkAge(age) {
  return (age > 18) ? true : confirm(' ?');
}

|| ( ):

function checkAge(age) {
  return (age > 18) || confirm(' ?');
}

, 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;
}

P.S. a == b , .

: 4

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

P.S. 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 && n % 1 == 0) {
  alert( pow(x, n) );
} else {
  alert(` ${n}  ,   `);
}


Web Proxy Viewer  |  New URL  |  Original Page