Is it possible to get the name of the function the code is executing in using JS?

For example:

function foo() {
    console.log(functionPame);
}

would output foo

or

function foo() {
    function bar() {
        console.log(functionPath)
    }
}

would output foo/bar

The format itself doesn't particularly matter, just that the information is available.

1 answer

  • answered 2021-05-14 16:09 Yair I

    you can use Error().stack, see example:

    i used this post: stack link

    function foo() {
        console.log(Error().stack);
    }
    
    function bla() {
        function bar() {
            console.log(Error().stack);
        }
        bar();
    }
    
    foo();
    bla();