13 JavaScript One-Liners That’ll Make You a Pro | Twan Mulder | DailyJS

By Twan Mulder

13 JavaScript One-Liners That’ll Make You Look Like a Pro

Twan Mulder

Image for post
Image for post
Photo by CHUTTERSNAP on Unsplash

JS code block showing how to use the Math Random method to get a random boolean value.
JS code block showing how to use the Math Random method to get a random boolean value.

const randomBoolean = () => Math.random() >= 0.5;console.log(randomBoolean());
// Result: a 50/50 change on returning true of false

JS code block showing how to write a function that will return if the provided day is a weekday or weekend day.
JS code block showing how to write a function that will return if the provided day is a weekday or weekend day.

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)

JS code block showing how to reverse a string.
JS code block showing how to reverse a string.

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

JS code block showing how to use the document hidden property to get if the current tab is in view or focus.
JS code block showing how to use the document hidden property to get if the current tab is in view or focus.

const isBrowserTabInView = () => document.hidden;isBrowserTabInView();
// Result: returns true or false depending on if tab is in view / focus

JS code block showing how to check if a number is even or odd using the modulo operator.
JS code block showing how to check if a number is even or odd using the modulo operator.

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

JS code block showing how to get the time from a date by using the toTimeString method and slicing the string.
JS code block showing how to get the time from a date by using the toTimeString method and slicing the string.

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

JS code block showing how to round a certain decimal point using the Math Power method.
JS code block showing how to round a certain decimal point using the Math Power method.

const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
// ExamplestoFixed(25.198726354, 1); // 25.1toFixed(25.198726354, 2); // 25.19toFixed(25.198726354, 3); // 25.198toFixed(25.198726354, 4); // 25.1987toFixed(25.198726354, 5); // 25.19872

toFixed(25.198726354, 6); // 25.198726

JS code showing how to check if an element is currently in focus using the activeElement property on the document object.
JS code showing how to check if an element is currently in focus using the activeElement property on the document object.

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

JS code block showing how to check if the current user has touch events supported.
JS code block showing how to check if the current user has touch events supported.

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

JS code block showing how you can check if the user is currently on an Apple device.
JS code block showing how you can check if the user is currently on 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

JS code block showing how the browser to the top using the scrollTo method.
JS code block showing how the browser to the top using the scrollTo method.

const goToTop = () => window.scrollTo(0, 0);goToTop();
// Result: will scroll the browser to the top of the page

JS code block showing how to use the reduce method to get an average value of the arguments.
JS code block showing how to use the reduce method to get an average value of the arguments.

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

JS code block showing how to convert Fahrenheit to Celsius and the other way around.
JS code block showing how to convert Fahrenheit to Celsius and the other way around.

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

celsiusToFahrenheit(-20); // -4

fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0