1. Get a random Boolean value (true/false)
This function returns a boolean value (true or false) using the Math.random() method. Math.random will create a random number between 0 and 1 , after which we check if it is above or below 0.5. This means that the odds of getting true or false are 50%/50%.
const randomBoolean = () => Math.random() >= 0.5;
console.log(randomBoolean());
// Result: a 50/50 change on returning true of false
2. Check whether the date is a working day
Using this method, you can check whether the function parameter is a weekday or a weekend.
const isWeekday = (date) => date.getDay() % 6 !== 0;
console.log(isWeekday(new Date(2021, 0, 11)));
// Result: true (Monday)
console.log(isWeekday(new Date(2021, 0, 10)));
// Result: false (Sunday)
3. Reverse the string
There are several different ways to reverse a string. The following code is one of the easiest ways.
const reverse = str => str.split('').reverse().join('');
reverse('hello world');
// Result: 'dlrow olleh'
4. Check whether the current Tab page is in the foreground
We can check if the current tab is in the foreground by using the document.hidden property.
const isBrowserTabInView = () => document.hidden;
isBrowserTabInView();
// Result: returns true or false depending on if tab is in view / focus
5. Check if the number is even
The easiest way to do this is by using the modulus operator (%). If you're not familiar with it, here's a nice illustration from Stack Overflow.
const isEven = num => num % 2 === 0;
console.log(isEven(2));
// Result: true
console.log(isEven(3));
// Result: false
6. Get time from date
By slicing the string at the correct position using the toTimeString() method, we can get the hour or current time from the provided date.
const timeFromDate = date => date.toTimeString().slice(0, 8);
console.log(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. Keep the decimal point (not rounded)
Using the Math.pow() method, we can truncate a number to a certain decimal point.
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
// Examples
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 the element is currently in focus
We can check whether an element is currently focused using the document.activeElement property.
const elementIsInFocus = (el) => (el === document.activeElement);
elementIsInFocus(anyElement)
// Result: will return true if in focus, false if not in focus
9. Check whether the browser supports touch events
const touchSupported = () => {
('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
}
console.log(touchSupported());
// Result: will return true if touch events are supported, false if not
10. Check whether the current user is an Apple device
We can use navigator.platform to check if the current user is an Apple device.
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
console.log(isAppleDevice);
// Result: will return true if user is on an Apple device
11. Scroll to the top of the page
The window.scrollTo() method takes an x and y coordinate to scroll. If we set these coordinates to zero, we can scroll to the top of the page.
const goToTop = () => window.scrollTo(0, 0);
goToTop();
// Result: will scroll the browser to the top of the page