The definitive source of the best
JavaScript libraries, frameworks, and plugins.

  • ×

    Javascript Astar

    A* Search / Pathfinding Algorithm in Javascript
    Filed under 

    • 🔾44%Overall
    • 1,315
    • 43 days
    • 🕩366
    • 👥11


    An implementation of the A* Search Algorithm in JavaScript

    See a demo at

    Sample Usage

    If you want just the A* search code (not the demo visualization), use code like this

    <script type='text/javascript' src='astar.js'></script>
    <script type='text/javascript'>
        var graph = new Graph([
        var start = graph.grid[0][0];
        var end = graph.grid[1][2];
        var result =, start, end);
        // result is an array containing the shortest path
        var graphDiagonal = new Graph([
        ], { diagonal: true });
        var start = graphDiagonal.grid[0][0];
        var end = graphDiagonal.grid[1][2];
        var resultWithDiagonals =, start, end, { heuristic: astar.heuristics.diagonal });
        // Weight can easily be added by increasing the values within the graph, and where 0 is infinite (a wall)
        var graphWithWeight = new Graph([
        var startWithWeight = graphWithWeight.grid[0][0];
        var endWithWeight = graphWithWeight.grid[1][2];
        var resultWithWeight =, startWithWeight, endWithWeight);
        // resultWithWeight is an array containing the shortest path taking into account the weight of a node

    A few notes about weight values:

    1. A weight of 0 denotes a wall.
    2. A weight cannot be negative.
    3. A weight cannot be between 0 and 1 (exclusive).
    4. A weight can contain decimal values (greater than 1).

    Original (slower) implementation

    The original version of the algorithm used a list, and was a bit clearer but much slower. It was based off the original blog post. The code is available at:

    The newest version of the algorithm using a Binary Heap. It is quite faster than the original. Binary Heap taken from (license:

    Running the test suite

    Build Status

    If you don't have grunt installed, follow the grunt getting started guide first.

    Pull down the project, then run:

        npm install
    Show All