let sel = document.getSelection();
let text = sel.toString();
console.log(text); // logs what the user selected
Alternatively, since the toString
member function is called automatically by some functions when converting the object to a string, you don't always have to call it yourself.
console.log(document.getSelection());