How do I write the result of a function into a variable?

  • 0
    There is a function that returns a string
    I want to write this line into a variable
    but if you write let abc = function (arg) then abc equals undefined
    JavaScript Anonymous, Jun 2, 2019

  • 3 Answers
  • 0
    function myFunc(arg) {
    return arg
    }

    let result = myFunc('Строка')
    console.log(result) // Строка
    Anonymous

  • 0
    but if you write let abc = function (arg), then abc is undefined


    This is exactly how you need to write. But the function must return a value.

    In this case, function is the conditional name of the function. Since this is a function word, you cannot use it for naming.

    Show the function itself.
    Anonymous

  • 0
    There is a function that returns a string

    ...

    if you write let abc = function (arg), then abc equals undefined


    Try it

    let abc = /* please give me a string */ function(arg)


    If it doesn't work, then only 3 options are possible:

    1. Most likely: this is a language bug, you have to accept and never use these buggy lines and functions.

    2. The function is asynchronous. The function returns a string sometime later.

    3. The function does not return a string. But wait, this can't be!
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!