"Delete" and "Backspace" keys action with Javascript

Often writing with Scribble / Apple Pencil makes some quick buttons useful.
Scripts below are working with or without a selection. Is this the right way to code them ?

// Delete key script 

let [st, len] = editor.getSelectedRange();
// select one char if nothing is selected
if (len == 0) {
 len++;
}
// replace selection with ""
editor.setTextInRange(st, len, "");
// unselect 
editor.setSelectedRange(st, 0);
editor.activate();


// Backspace key script 

let [st, len] = editor.getSelectedRange();
// select previous char if nothing is selected
if (len == 0) {
 	len++;
 	st--;
}
// replace selection with ""
editor.setTextInRange(st, len, "");
// unselect 
editor.setSelectedRange(st, 0);
editor.activate();