Get the line number of the currently executing code in JavaScript



Get line number

JavaScript does not directly provide a function to get the line number, but we can find that when the code goes wrong, we can see the detailed error message in the console. The error message stack will contain the specific calling process and the location information of the file. With this feature, we can write a function, instantiate an error object in the function, and get the caller's line number through the stack information.

            function getRowNum() {
    let e = new Error();
    e = e.stack.split("\n")[2].split(":");
    return e.pop();