New Jersey has the best state education system in the country thanks largely to the high amount of money it spends on its schools, according to a new national ranking.

Education Week, a national industry publication, looked at 39 categories, including school finance, academic achievement and socioeconomic factors, to rank the states’ school systems for its annual “Quality Counts” report.

Massachusetts has come out on top in the last few years. But, this year New Jersey pulled slightly ahead in a “photo finish,” according to the ranking.

It is the first time New Jersey has ever ranked No. 1 in Education Week’s analysis. The state scored a grade of 87.8 -- or a B+.

“Massachusetts fell short of the top spot by just a few hundredths of a point on the combined scores,” Education Week said.

Massachusetts was followed by Connecticut, Maryland and New Hampshire in the top spots.

New Jersey was able to pull ahead partly because of the amount of money it spends on its taxpayer-funded school systems, the report said.

“New Jersey has an advantage over Massachusetts in both spending and equity. It ranks sixth for per-pupil expenditures at $16,543, while Massachusetts is 13th at $14,529 once figures are adjusted for regional cost differences,” the report said.

New Jersey devotes 4.8 percent of its total tax dollars to education, the third-highest in the nation, and one of the reasons it topped the list this year, the analysis said.

New Jersey also did a better job of making sure funding was more evenly distributed between wealthy and poor school districts, according to the statistics.

At Wednesday’s state Board of Education meeting in Trenton, state Education Commissioner Lamont Repollet said he was extremely proud to hear about New Jersey’s ranking, but not surprised.

“For years, we have been ranked No. 1 in our mind because of the work that we do,” Repollet said.

Less wealthy states continued to score poorly with Mississippi ranking as the worst state in the nation for education with a score of 68.5 -- or a D+.

Louisiana, Oklahoma, Nevada and New Mexico were also at the bottom of the list.

Overall, the United States earned a grade of C for its education system.

“Major differences in educational performance separate high-performing school systems in the Northeast and Mid-Atlantic regions from lower-performing systems elsewhere in the nation,” the report said.

“Where you grow up has an impact on the educational opportunities available to you,” according to the analysis.

Staff writer Adam Clark contributed to this report.

Kelly Heyboer may be reached at kheyboer@njadvancemedia.com. Follow her on Twitter @KellyHeyboer. Find her at KellyHeyboerReporter on Facebook. Find NJ.com on Facebook.

Have a tip? Tell us. nj.com/tips

Get the latest updates right in your inbox. Subscribe to NJ.com’s newsletters.