How to get info of object within certain range?(Javascript)

So here is the idea:

  • My AI moves forward constantly.
  • Once object is in range with AI information is send in order to make ray.
  • And once my AI moves away from object ray disappears.

Any ideas for script.