how to get selected_text variable inside Apple Script

I used save selected text to variable function and know I wanna work with that text inside Apple Script or javascript how can I do that?

Basically, I wanna get current text selected and reverse that text then paste in the same place.

Thank you.