2018 version - prepend
parent.prepend(newChild)  // [newChild, child1, child2]
This is modern JS! It is more readable than previous options. It is currently available in Chrome, FF, and Opera.
The equivalent for adding to the end is append, replacing the old appendChild
parent.append(newChild)  // [child1, child2, newChild]
Advanced usage
- You can pass multiple values (or use spread operator ...).
- Any string value will be added as a text element.
Examples:
parent.prepend(newChild, "foo")   // [newChild, "foo", child1, child2]
const list = ["bar", newChild]
parent.append(...list, "fizz")    // [child1, child2, "bar", newChild, "fizz"]
Related DOM methods
- Read More - child.beforeandchild.after
- Read More - child.replaceWith
Mozilla Documentation
Can I Use