Get selected text as string

Here's a useful JavaScript code snippet to get the text that a user selects or highlights on a web page.

const getSelectedText = () => window.getSelection().toString();

How to use

This code snippet is useful for building a toolbox that can be used to share bits of an article to social or to save as a note in your database.