Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the current line number in JavaScript?

Does JavaScript have a mechanism for determining the line number of the currently executing statement (and if so, what is it)?

like image 570
Matthew Murdoch Avatar asked Feb 26 '10 17:02

Matthew Murdoch


People also ask

What is $() in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

Is JavaScript line by line execution?

JavaScript is a single-threaded language, where only one command executes at a time. It has a Synchronous model of execution, where each line is executed line by line, top to bottom.

How do you skip a line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.


1 Answers

var thisline = new Error().lineNumber

If that doesn't work in whatever environment you're using, you can try:

var stack = new Error().stack

Then hunt through the stack for the line number.

like image 130
Mark Bolusmjak Avatar answered Sep 28 '22 02:09

Mark Bolusmjak