Line counter for code — total, code, comment and blank lines

Paste a file or snippet to see how many lines it has and how many of them are code, comments or blank, with the comment syntax detected for you.

Live demo · real result from this tool
Total lines10
Code lines
5
Comment lines
4
Blank lines
1
Characters
212
Longest line
70 characters
Average line
20.2 characters
Comment syntax
c

Processed on your device — nothing is uploaded.

How to use the line counter

  1. Paste code or text into the Code box. The sample is a short JavaScript file.
  2. Leave Comment syntax on Detect, or choose one: // and /* */, #, -- and /* */ (SQL), <!-- -->, -- (Lua, Haskell) or No comments.
  3. Read the counts: Total lines, Code lines, Comment lines, Blank lines, Characters, Longest line, Average line and the Comment syntax used.

What it does and when to use it

Line counts answer quick questions: how big is this function, how much of a file is comments, does a snippet fit a “max 50 lines” rule, or which line breaks an 80- or 120-column limit. The code/comment split helps when you review documentation coverage or estimate effort for a port. Longest line shows at once whether any line is too wide for your style guide.

To remove the blank lines it finds, use the blank line collapser. The whitespace normalizer fixes trailing spaces and line endings, and the code diff viewer counts added and removed lines between two versions.

How it works

The text is split into lines (CRLF, CR and LF all count as breaks). Each line is then read character by character with the chosen comment rules, and the counter keeps track of whether it is inside a string or an open block comment:

  • A quote starts a string, so // or # inside a string is not a comment. The sample’s "https://api.example.com//users" is counted as code.
  • /* or <!-- opens a block comment that can span lines; every line inside it is a comment line.
  • Any other non-space character makes the line a code line.

Characters are counted as visible characters, so an emoji counts as one, and line breaks are included. Everything happens in your browser.

Worked examples

These are the tool’s own outputs.

The JavaScript sample. 10 lines: 5 code, 4 comment (one // line and a three-line /* */ block) and 1 blank. The line with // not a comment start after code counts as code. Its longest line is 70 characters.

A Python script. With a shebang, a # Count words comment, two blank lines and a docstring, the result is Total 8, Code 4, Comment 2, Blank 2, and Detect picks hash.

A SQL query. -- monthly report followed by a three-line query is detected as sql: 3 code lines and 1 comment line. The /* paid only */ after FROM orders does not make that line a comment.

HTML. A one-line <!-- header --> and a three-line comment block give 4 comment lines and 3 code lines.

Limits and tips

  • Detect is a guess. Shell scripts with -- options or SQL inside strings can fool it; set the syntax by hand.
  • Nested block comments are not tracked; the first */ closes the comment.
  • Strings across lines are followed only for backtick template literals.

Frequently asked questions

How is a line with code and a comment counted?
As a code line. A line counts as a comment line only when it holds nothing but comment text, and as blank only when it holds nothing but spaces or tabs. Every line lands in exactly one group, so code, comment and blank lines add up to the total.
Are Python docstrings counted as comments?
No. A docstring is a string, so its lines count as code here. Some line-counting tools, such as cloc, count docstrings as comments instead, so totals can differ. Lines starting with # count as comments, including a #! shebang line.
Does a trailing newline add a line?
No. A final line break at the end of the file is not counted as an extra empty line, so a file that ends neatly with a newline is not over-counted.
How does Detect choose the comment syntax?
It checks the text for clues in this order. Any HTML comment means HTML or XML; Python-style lines such as def or import with no // or /* mean hash comments; a -- comment line or SELECT … FROM means SQL; anything else uses // and /* */. Pick the syntax yourself if the guess is wrong.
Can it count a whole project?
No, it counts one pasted text at a time. For a repository, use a command-line tool such as cloc or tokei.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close