Array
Check whether array empty
- No need to check whether array is undefind anymore, just use null conditional operator.
if (array !=== undefined && array.length != 0){}
if (array?.length){}
forEach
array.forEach( () => {} )
filter
array.filter( () => {} )
Check whether an array contains all the elements in another array (tag check)
required_tags = ['A', 'B']
doc_tags = ['A', 'B', 'C']
if (requried_tags.every((tag) => doc_tags.includes(tag)))
Custom attribute
HTML element are all just javascript objects, we can add custom attributes on it if needed.
The convention is to add ‘data-’ prefix on the attribute name.
let tagButtonsContainer = document.getElementsByClassName("tagSearch-tags");
tagButtonsContainer.searchTags = [];
Javascript object vs JSON
Unlike JSON, in JS, we don’t need to wrap object keys in double quotes.