Sometimes, we want to replace the default right-click menu with our own menu that allows user to perform additional actions.
Add characters to the end of a string if it’s less than a certain length
Accepts two arguments: the length the string should be, and what characters to add if it’s not that length.
Find a Specific Element from an Array
Use find() method to find an element matching a specific criterion
Check if a date is the same as another date
Use Date.prototype.toISOString() and strict equality checking (===) to check if the first date is the same as the second one.
Scale a text to fit inside of an element
First of all, we need to measure the width of element with its current font size and text content.
Add characters to the beginning of a string if it’s less than a certain length
Accepts two arguments: the length the string should be, and what characters to add if it’s not that length.
Short-Circuit Evaluation Using &&
Instead of having to check if something is true with an if statement, you can use the && operator:
Calculate the midpoint between two pairs of (x,y) points
Destructure the array to get x1, y1, x2 and y2. Calculate the midpoint for each dimension by dividing the sum of the two endpoints by 2.
Resize the width of a text box to fit its content automatically
To adjust its width based on its content dynamically, we create a fake element whose content is the same as the input value.
Check if an element has an attribute
Check for the existence of an attribute (including data attributes) on an element. This method can also be used to manipulate other types of attributes—things like id, tabindex, name, and so on.