LOS ANGELES — Jared Goff turned a phenomenal first half into the biggest game of his career, and it was still just barely enough to beat Kirk Cousins.

Goff passed for career highs of 465 yards and five touchdowns, winning a scintillating duel with his Minnesota counterpart and leading the unbeaten Rams to a 38-31 victory over the Vikings on Thursday night.

Goff hit Cooper Kupp with two of his four TD throws during a 251-yard first half in which the third-year quarterback flawlessly executed coach Sean McVay’s offense.

“It was just great command by him,” McVay said about Goff. “I think he’s having fun. No moment is too big for him, and it gives you confidence when you see him playing like this.”

Todd Gurley then finished with 83 yards rushing and 73 yards receiving while the Rams (4-0) held off the Vikings (1-2-1) and Cousins, who passed for 422 yards and three touchdowns.

The Vikings got the ball back with 2:20 left, but Los Angeles rookie John Franklin-Myers stripped Cousins of the ball near midfield. The Rams recovered and ran out the clock on their first win in the franchise’s last six meetings with Minnesota.

“It feels really good,” Goff said. “You work for that. It’s what you strive for. We got into that zone in the first half, and we’re really clicking right now.”

McVay was Cousins’ offensive coordinator in Washington before he became Goff’s head coach on the West Coast last season. McVay’s current and former prize pupils put on a show at the Coliseum, but Goff finished 26 of 33 with a perfect 158.3 quarterback rating.

“It’s fun for us, personally,” Goff said of his duel with Cousins. “I don’t know if it’s fun for the whole team, but it’s cool. Kirk is a guy that we watched on film forever last year. … To go against him and kind of duke it out, I told him after the game that I appreciated it. I’ve got a lot of respect for him and the way he plays.”

Goff picked apart the Vikings’ secondary in the first half with one pinpoint throw after another. The former No. 1 pick has three consecutive 300-yard games during the Rams’ perfect start.

“I am very glad Jared is on my side of the ball,” said Rams defensive tackle Ndamukong Suh, who had a key second-half sack. “I wouldn’t want to go against him.”

Gurley and Brandin Cooks had first-half TD catches for the Rams, but the Vikings trimmed LA’s lead to 31-28 late in the third quarter with Adam Thielen’s 45-yard TD catch and a 2-point conversion.

Goff promptly surpassed his previous career highs for yards passing during a swift drive ending in Robert Woods’ 31-yard TD catch, and the Rams’ defense hung on while the offense went scoreless in the fourth quarter.

“We did not play well defensively,” Vikings coach Mike Zimmer said. “Offensively we moved the ball well, but we had too many missed tackles, and too many people were left wide open. I’ve not ever had a team that’s been this poor in pass coverage. We are going to have to look at everything.”

Both offenses came out firing in front of a lively Coliseum crowd that included a healthy throng of purple-clad fans enjoying the Vikings’ first game in Los Angeles since 1993.

One week after giving up 27 points in the first half and getting blown out as 16 1/2-point favorites in Buffalo, the Vikings gave up 28 before halftime against LA. They still hung in thanks to Cousins, who hit Aldrick Robinson for two first-half scores.

Goff found Kupp with two beautiful TD throws in the first half, the first hitting Kupp in stride on a 70-yard sprint , and the second eluding two Vikings and dropping into Kupp’s hands deep in the end zone.

“Jared put it in about a 6-inch box that only he could fit it into,” McVay said.

Less than 2 1/2 minutes later, Goff hit Cooks down the middle for a 47-yard score.

Both of those throws beat Minnesota cornerback Trae Waynes, who missed the second half with a concussion.

Goff’s four TD passes were the most in a first half by a Rams quarterback since Kurt Warner did it in 1999.

Sam Ficken, the replacement for injured Rams All-Pro kicker Greg Zuerlein, kept it close by missing a 28-yard field goal with 8:21 to play.

Minnesota’s Dan Bailey hit a 40-yard field goal with 3:46 left for the only points of the fourth quarter.