JavaScript – simple Array difference


JavaScript – simple Array intersection


JavaScript – get caret position in contentEditable

The Range instance returned by various Selection methods – most commonly sindow.getSelection().getRangeAt(0) – does not provide enough information to determine the total offset of the caret (or selection start/end). Here’s how:


JavaScript – strip comments from JavaScript

Since the online comment-strippers I found didn’t work for the most part, here’s a little working POC of that _should_ work on any valid JS input. Paste the JS into the textarea and click the button – the textarea’s value will be updated with the stripped version of the script you pasted. Strip Comments

JavaScript – classList polyfill

So in searching for a good polyfill for Element.classList, I found 2 promising versions: 1., which is on MDN as a “suggested” polyfill: This definitely works, but IMO is a little inelegantly written and might be hard to follow. 2., which is nice and clean, but the contains method is broken (I […]

JavaScript – simple implementation of EventTarget

From the EventTarget interface described here: /UPDATE 04/21/16 – this had a bug in the first line of dispatchEvent – updated now.


JavaScript – initializing variables without default values

Assuming we’ve all read, still might be some surprises:

No surprise, throws an Error.

No surprise, logs undefined (no error)

Surprise – *both* log undefined, no error is thrown, even on the first line before bob is “initialized”.

Surprise, Logs false.

Surprise, logs true, despite it being functionally equivalent […]

JavaScript – Simple Unique Array

FWIW, I remember reading this idea somewhere else, probably stack, so credit to whoever first figured this out.


JavaScript – Simple Copy and Clone

There’s a bunch of ways to do this. I think what follows is the most simple, logical, portable and transparent. Copy:


Packaged up:

Simple HTML (string) to DocumentFragment (usable Node)