
Here you will find the Most commonly used Javascript one liners to ease your daily coding stuff.
- Select all elements with a particular class: document.querySelectorAll(‘.className’);
- Get the current URL: window.location.href;
- Detect if an element is visible on the screen:
let element = document.querySelector(‘#some_element’);
element.getBoundingClientRect().top < window.innerHeight - Check if a string contains another string: (str.includes(subString))
- Convert a NodeList to an array: […nodeList]
- Load external scripts dynamically:
let script = document.createElement(‘script’);
script.src = ‘path/to/script.js’;
document.body.appendChild(script); - Get the page title: document.title;
- Copy text to clipboard: navigator.clipboard.writeText(data)
- Format a number with specific decimal points: new Intl.NumberFormat(‘en-US’, {maximumFractionDigits: 2}).format(100.1234);
- Get current date and time: new Date().toISOString();