What does return mean in JavaScript?


While learning JavaScript, I got to return. I didn't understand what it was for. At first, I tried to "add" something, then look for explanations in Google, but everything was too sophisticated. What does it mean to return a result? What is return for? And a bunch of different other questions to all this. I hope that you will explain everything in your own words and thank you in advance

Author: aleksandr barakin, 2018-12-30

2 answers

//функция calculate принимает на вход два параметра: a, b
//выполняет работу (складывает эти два числа) и результат возвращает (для этого служит оператор return)
//после return указывается что именно надо возвратить
//если не нравится слово возвращает, можешь заменить его словом 'выдает'
function calculate(a, b) {
  x = a + b;
  return x;
}

//так как функция возвращает результат, то его можно записать в переменную number
//мы передаем в функцию calculate два числа: 2 и 3
//она их складывает и возвращает(выдает) результат 5
//этот результат записывается в переменную number
var number = calculate(2, 3);

//теперь в консоли мы можем посмотреть чему равна переменная number
console.log(number);
 4
Author: Vincent, 2018-12-30 19:33:27

The function has an input and an output, the function receives arguments at the input, and outputs the result at the output. Here, return just means exiting the function and issuing the result.

 3
Author: legale, 2018-12-30 19:35:23