Read contents of frontmost open file and update it

I want to write a script where I can read the lines of the front most open JavaScript file and update a variable in this file.
The file contains the following 3 lines. The testToRun variable is assigned the value of test1 initially

I’m interested in assigning test2 value to the testToRun variable using Applescript.

So when my applescript is run, it should loop through the lines of the front most open JS file, and change last line in my JS file to testToRun=test2 and save it.

Dont ask me why I’m interested in doing this. This is a small part of a major script I’m about to write and I’m stuck at this step. The JS file is a part on an application. Here is small snippet

tell application "XYZ"
	tell front document -- The open JS file
		-- Logic to loop through lines of the JS file
	end tell
end tell