DevTools Logo

JavaScript AST Visualizer

JavaScript AST Visualizer

Parse JavaScript with acorn into a navigable ESTree tree, locally in your browser.

Runs in your browser
JavaScript only: no TypeScript or JSX.
Try an example

Source

3 lines · 56 chars

AST

Working…

Paste JavaScript source to inspect its AST hierarchy. Set a depth limit for large programs, review node type counts, and copy deterministic recursive JSON. TypeScript syntax is intentionally unsupported and reports a location-aware parser error.

    Examples

    Function count

    Input
    visualizeJavaScriptAst("function foo() { return 1; }").counts
    Output
    { FunctionDeclaration: 1, ... (keys sorted) }

    Every node type is counted once per visit with keys in alphabetical order.

    Depth truncation

    Input
    visualizeJavaScriptAst("const value = { nested: { answer: 42 } }", 1)
    Output
    { tree.children[0].truncated: true, counts.ObjectExpression: undefined }

    At maxDepth 1 the first child truncates and deeper object nodes are never visited.

    About this tool

    JavaScript AST Visualizer parses modules with visualizeJavaScriptAst(source, maxDepth = 20) (alias parseJavaScriptAst): acorn compiles with ecmaVersion latest and sourceType module, then createTree walks every node and array member into an AstTreeNode tree of { type, children, truncated? }.

    Counts tally every visited type and return sorted alphabetically, and json is JSON.stringify(tree, null, 2) — so FunctionDeclaration, VariableDeclaration, and Program counts plus the JSON string are identical across runs for identical input.

    Limits and errors are explicit: non-integer or negative maxDepth throws, nodes at depth >= maxDepth return truncated true without traversing deeper, and acorn failures rethrow as JavaScriptAstError with one-based line and column in the message.

    How to use this tool

    1. Paste JavaScript

      Provide module source — imports and exports parse, TypeScript annotations do not.

    2. Set depth

      Lower maxDepth to collapse deep subtrees into truncated markers.

    3. Read counts

      Use the sorted counts map to spot node density and recursion depth.

    4. Export JSON

      Copy the deterministic JSON tree for snapshots or teaching diagrams.

    Use cases

    Teaching syntax trees

    Show how functions, declarations, and expressions nest in real code.

    Auditing complexity

    Compare node counts across refactors to spot growing branches.

    Debugging parsers

    Pinpoint acorn syntax errors by exact line and column.

    Common mistakes

    Mistake:Pasting TypeScript and expecting a tree.

    Fix:acorn rejects type annotations — strip : string style syntax or transpile first.

    Mistake:Reading truncated subtrees as complete.

    Fix:truncated true means children were cut at maxDepth — raise the limit to see deeper nodes.

    Frequently asked questions

    What does this tool do?

    It runs visualizeJavaScriptAst on module source with acorn, returning a node tree, alphabetically sorted type counts, and a deterministic JSON string.

    How do depth limits and errors work?

    Nodes at depth >= maxDepth return truncated true without deeper visits, non-integer or negative depths throw, and syntax failures rethrow as JavaScriptAstError with one-based line and column.

    Is my code uploaded or stored?

    No. Acorn parsing and tree rendering run fully client-side.

    Why did TypeScript fail, or counts miss a node?

    Acorn rejects type annotations, and truncated subtrees are excluded from counts by design — raise maxDepth to include deeper ObjectExpression style nodes.

    References & standards