13 JavaScript one liners make you look like an expert

  • 2021-11-13 06:16:20
  • OfStack

Directory 1. Get a random Boolean value (true/false) 2. Check whether the date provided is a workday 3. Invert the string 4. Check whether the current label is hidden 5. Check whether a number is even or odd 6. Get the time from a date 7. Keep the n decimal 8. Check whether an element is currently in focus 9. Check whether the current browser supports touch events 10. Check the current browsing Scroll to the top of the page 12. Get the average value of the parameter 13. Fahrenheit/Celsius conversion

1. Get a random Boolean value (true/false)

This function uses the Math.random() Method returns 1 Boolean value ( true Or f alse ). Math. random creates a random number between 0 and 1, and we only need to check whether it is higher or lower than 0.5, and we have a 50% chance of getting true or false.


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

2. Check whether the date provided is a working day

Using this method, we can check whether the date provided in the function is a working day or a weekend day.


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

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

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

3. Invert the string

There are several different ways to reverse a string. This is the simplest one, using the split (), reverse (), and join () methods.


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

4. Check whether the current tag is hidden

Document.hidden (Read-only property) returns a Boolean value indicating that the page is ( true ) No ( false ) Hide.


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

Off-site: I inadvertently discovered that the playing time of iQiyi advertisement will be counted down when the current tab is activated. When I leave the current tab, the countdown stops and Baidu 1 finds it document.hidden This thing.

document.hidden There is a compatibility problem when h5 is newly added to api.


var hidden
if (typeof document.hidden !== "undefined") {
    hidden = "hidden";
} else if (typeof document.mozHidden !== "undefined") {
    hidden = "mozHidden";
} else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
} else if (typeof document.webkitHidden !== "undefined") {
    hidden = "webkitHidden";
}
console.log(" Whether the current page is hidden: " + document[hidden])

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 time from 1 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. Keep the n decimal


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 any elements are 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 not in focus, return  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, the true Or return if it is not supported false . 

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


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

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

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

11. Scroll to the top of the page


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

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

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

12. Get the average value of the parameter


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

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

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

13. Fahrenheit/Celsius conversion


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

Related articles: