Skip to content

Instantly share code, notes, and snippets.

@danielcardeenas
Last active August 3, 2021 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielcardeenas/56d5f6a6836dbbea3d1ff76f22f43c44 to your computer and use it in GitHub Desktop.
Save danielcardeenas/56d5f6a6836dbbea3d1ff76f22f43c44 to your computer and use it in GitHub Desktop.
Javascript utility one-liners
// 1. Get a random boolean (true/false)
const randomBoolean = () => Math.random() >= 0.5;
randomBoolean();
// Result: a 50/50 change on returning true of false
// 2. Check if the provided day is a weekday
const isWeekday = (date) => date.getDay() % 6 !== 0;
isWeekday(new Date(2021, 0, 11));
// Result: true (Monday)
// Result: false (Sunday)
// 3. Reverse a string
const reverse = str => str.split('').reverse().join('');
reverse('hello world');
// Result: 'dlrow olleh'
// 4. Check if the current tab is in view / focus
const isBrowserTabInView = () => document.hidden;
isBrowserTabInView();
// Result: returns true or false depending on if tab is in view / focus
// 5. Check if a number is even or odd
const isEven = num => num % 2 === 0;
isEven(2)
// Result: true console.log(isEven(3));
// Result: false
// 6. Get the time from a date
const timeFromDate = date => date.toTimeString().slice(0, 8);
timeFromDate(new Date(2021, 0, 10, 17, 30, 0));
// Result: "17:30:00" console.log(timeFromDate(new Date()));
// Result: will log the current time
// 7. Truncate a number to a fixed decimal point
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
toFixed(25.198726354, 1); // 25.1
toFixed(25.198726354, 2); // 25.19
toFixed(25.198726354, 3); // 25.198
toFixed(25.198726354, 4); // 25.1987
toFixed(25.198726354, 5); // 25.19872
toFixed(25.198726354, 6); // 25.198726
// 8. Check if an element is currently in focus
const elementIsInFocus = (el) => (el === document.activeElement);
elementIsInFocus(anyElement)
// Result: will return true if in focus, false if not in focus
// 9. Check if the current user has touch events supported
const touchSupported = () => {
('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
}
touchSupported();
// Result: will return true if touch events are supported, false if not
// 10. Check if the current user is on an Apple device
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
isAppleDevice;
// Result: will return true if user is on an Apple device
// 11. Scroll to top of the page
const goToTop = () => window.scrollTo(0, 0);
goToTop();
// Result: will scroll the browser to the top of the page
// 12. Get average value of arguments
const average = (...args) => args.reduce((a, b) => a + b) / args.length;
average(1, 2, 3, 4);
// Result: 2.5
// 13. Convert Fahrenheit / Celsius
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment