current position:Home>13 cool JavaScript one-line programs

13 cool JavaScript one-line programs

2021-08-23 05:35:52 Magi Huangyuan

1. Get a random Boolean value (true/false

const randomBoolean = () => Math.random() >= 0.5;
console.log(randomBoolean());

2. Check that the date provided is a working day

getDay() Method returns a week (0~6) The number of a certain day .

Be careful :  Sunday is 0, Monday is 1, And so on .

const isWeekday = (date) => date.getDay() % 6 !== 0;

console.log(isWeekday(new Date(2021, 7, 6)));
// true   Because it's Friday 

console.log(isWeekday(new Date(2021, 7, 7)));
// false  Because it's Saturday 

3. Reverse string  

const reverse = str => str.split('').reverse().join('');
reverse('hello world');     
// 'dlrow olleh'

4. Check whether the current tag is hidden

Outside : Inadvertently found that iqiyi's advertisement playing time actually counts down when the current tab is activated , When leaving the current tab , The countdown stops , Baidu found document.hidden This east east .

Document.hidden ( Read-only property ) Returns a Boolean value , Indicates that the page is (true) no (false) hide .

const isBrowserTabInView = () => document.hidden;
isBrowserTabInView();

5. Check whether a number is even or odd

const isEven = num => num % 2 === 0;
console.log(isEven(2));
// true
console.log(isEven(3));
// false

6. Get the time from a date

const timeFromDate = date => date.toTimeString().slice(0, 8);

console.log(timeFromDate(new Date(2021, 0, 10, 17, 30, 0))); 
// "17:30:00"

console.log(timeFromDate(new Date()));
//  Print the current time 

7. Retain n Decimal place

const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
//  Example 
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 any element is currently in focus

We can use document.activeElement Property to check whether an element is currently in focus .

const elementIsInFocus = (el) => (el === document.activeElement);
elementIsInFocus(anyElement)
//  If you return in focus true, If you don't return in focus  false

9. Check whether the current browser supports touch events

const touchSupported = () => {
  ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
}
console.log(touchSupported());
//  If touch events are supported , Will return true, If not, return false.

10. Check whether the current browser is on the Apple device

const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
console.log(isAppleDevice);

11. Scroll to the top of the page

const goToTop = () => window.scrollTo(0, 0);
goToTop();

12. Get the average value of the parameter

const average = (...args) => args.reduce((a, b) => a + b) / args.length;
average(1, 2, 3, 4);
// 2.5

13. Fahrenheit / Celsius conversion

const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
//  Example 
celsiusToFahrenheit(15);    // 59
celsiusToFahrenheit(0);     // 32
celsiusToFahrenheit(-20);   // -4
fahrenheitToCelsius(59);    // 15
fahrenheitToCelsius(32);    // 0
 
 
 
 
 

copyright notice
author[Magi Huangyuan],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210823053549481r.html

Random recommended