The original way to add elements to the DOM. Call it on the parent of the element you’re inserting your new element before (the referenceNode), and pass in both the new element and the reference node as arguments. You can also use it to inject an element after another one by using the .nextSibling property on your referenceNode. Source https://vanillajstoolkit.com/reference/dom-injection/element-insertbefore/
Miscellaneous
Remove falsy values from an array
Removes falsy values from an array. Use Array.prototype.filter() to filter out falsy values (false, null, 0, “”, undefined, and NaN). Source https://www.30secondsofcode.org/js/s/compact
How to change the value of an object which is inside an array
Source https://github.com/roeib/JavaScript-snippets#how-to-change-the-value-of-an-object-which-is-inside-an-array
Create an element
Source https://vanillajstoolkit.com/reference/dom-injection/createelement/
Get and set the text of an element (without markup)
Get and set the text of an element without its markup. Source https://vanillajstoolkit.com/reference/html/textcontent/
Get and set HTML content for an element
Note: using innerHTML with third-party or user-submitted content can expose you to cross-site scripting (XSS) attacks. Source https://vanillajstoolkit.com/reference/html/innerhtml/
Remove an item from an object
Use the delete operator on the key to remove. Source https://vanillajstoolkit.com/reference/objects/delete/
Return an array of elements that appear in both arrays
Use Array.prototype.includes() to determine values that are not part of values. Use Array.prototype.filter() to remove them. Source https://www.30secondsofcode.org/js/s/similarity
Check if a string is a valid JSON
This is useful for making sure that the JSON is valid before you perform another operation on it. You can learn more about how to use json parse on WebReference. While working, this json formatter and validator is a handy tool. Source https://github.com/roeib/JavaScript-snippets#check-if-a-string-is-a-valid-json
Perform a shallow merge of two or more objects
Perform a shallow merge of two or more objects into the first. Pass in each object to merge as an argument. Note: in a shallow merge, nested objects are overwritten completely rather than having their values merged together. All objects are merged into the first. To create a new object, pass in an empty object as […]