GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All gists
  • Back to GitHub
  • Sign in
  • Sign up
  • Code
  • Revisions 2
  • Stars 1
  • Terms
  • Privacy
  • Security
  • Status
  • Community
  • Docs
  • Contact

This view

  • Original page
  • New address

GeezerVison is showing gist.github.com — 20 JavaScript One-Liner Functions You Need to Know

Instantly share code, notes, and snippets.

@ArsenTech

ArsenTech/javascript-one-liners.js

Last active December 24, 2025 16:34
Show Gist options
  • Download ZIP
  • Star 1 (1) You must be signed in to star a gist
  • Fork 0 (0) You must be signed in to fork a gist
  • Clone this repository at <script src="https://gist.github.com/ArsenTech/f9d2b599cb758c889938a8c943860a57.js"></script>
  • Save ArsenTech/f9d2b599cb758c889938a8c943860a57 to your computer and use it in GitHub Desktop.
Clone this repository at <script src="https://gist.github.com/ArsenTech/f9d2b599cb758c889938a8c943860a57.js"></script>
Save ArsenTech/f9d2b599cb758c889938a8c943860a57 to your computer and use it in GitHub Desktop.
Download ZIP
20 JavaScript One-Liner Functions You Need to Know
Raw
javascript-one-liners.js
// Get a random number between two values.
const getRandomNumber = (min, max) => Math.floor(Math.random()*(max-min))+min;
console.log(getRandomNumber(1,10))
// Calculate the factorial of a given number.
const factorial = number => number===1 ? number : number*factorial(number-1);
console.log(factorial(7))
// Generate a random item from an array.
const getRandomValue = arr => arr[Math.floor(Math.random()*arr.length)];
console.log(getRandomValue(["a","b","c","d","e","f","g","h"]))
// Shuffle an array.
const shuffle = arr => arr.sort(()=>0.5-Math.random());
console.log(shuffle([0,1,2,3,4,5,6,7,8,9]))
// Check if a number is positive.
const isPositive = num => num > 0;
console.log(isPositive(5))
console.log(isPositive(-5))
// Get even numbers from an array.
const getEvenNumbers = arr => arr.filter(num=>num%2===0);
console.log(getEvenNumbers([0,1,2,3,4,5,6,7,8,9]))
// Find the average of an array.
const getAverage = arr => (arr.reduce((acc,num)=>acc+num,0))/arr.length;
console.log(getAverage([0,1,2,3,4,5,6,7,8,9]))
// Check if a given array is empty.
const isEmpty = arr => arr.length === 0;
console.log(isEmpty([]))
console.log(isEmpty([1,2,3]))
// Count the vowels in a string.
const countVowels = str => (str.match(/[aeiou]/gi) || []).length;
console.log(countVowels("ArsenTech"))
// Reverse a given string.
const reverseString = str => str.split("").reverse().join("");
console.log(reverseString("Hello World"))
// Get odd numbers from an array.
const getOddNumbers = arr => arr.filter(num=>num%2!==0);
console.log(getOddNumbers([0,1,2,3,4,5,6,7,8,9]))
// Check if a number is even.
const isEven = num => num % 2 === 0;
console.log(isEven(1));
console.log(isEven(2));
// Convert a given string to an array of words.
const toWordsArray = str => str.split(" ");
console.log(toWordsArray("Lorem Ipsum Dolor Sit amet"))
// Remove duplicates from an array.
const getUniqueItems = arr => [...new Set(arr)];
console.log(getUniqueItems([0,1,1,2,3,4,4,5,6,7,8,8,8,9,9,9,9,1,2,3,3,3,3,3]))
// Capitalize a given string.
const capitalize = str => str[0].toUpperCase() + str.slice(1);
console.log(capitalize("arsenTech"))
// Check if a number is odd.
const isOdd = num => num % 2 !== 0;
console.log(isOdd(2));
console.log(isOdd(3))
// Calculate the area of a circle.
const getCircleArea = rad => Math.PI*rad**2;
console.log(getCircleArea(8))
// Sum all numbers in an array.
const sum = arr => arr.reduce((acc,num)=>acc+num,0);
console.log(sum([0,1,2,3,4,5,6,7,8,9]))
// Check if a string is a palindrome.
const isPalindrome = str => str === str.split("").reverse().join("");
console.log(isPalindrome("kayak"))
console.log(isPalindrome("coding"))
// Convert Celsius to Fahrenheit.
const toFahrenheit = celsius => (celsius*9/5)+32;
console.log(toFahrenheit(30))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You are visitor 01402639 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.