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

In just a few minutes, step up your JS knowledge.

Twan Mulder
Jan 19 · 5 min read
Photo by CHUTTERSNAP on Unsplash

1. Get a random boolean (true/false)

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

2. Check if the provided day is a weekday

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)

3. Reverse a string

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'

4. Check if the current tab is in view / focus

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

5. Check if a number is even or odd

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

6. Get the time from a date

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

7. Truncate a number to a fixed decimal point

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);// 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 an element is currently in focus

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

10. Check if the current user is on an Apple device

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

11. Scroll to top of the page

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

12. Get average value of arguments

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

13. Convert Fahrenheit / Celsius

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;// Examples
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4
fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0

DailyJS

JavaScript news and opinion.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store