20+ Quirky JavaScript Functions That Are Oddly Genius
JavaScript is a language where creativity knows no bounds, often leading to quirky, unconventional solutions that are surprisingly effective. Here's a list of 20+ bizarre but functional JavaScript functions that will make you scratch your head, laugh, and appreciate the brilliance hidden in their absurdity.
1. Determine If a Number Is Even
function isEven(n) {
let absN = Math.abs(n);
let out = true;
for (let i = 0; i < absN; i++) {
out = !out;
}
return out;
}
This genius function flips a boolean repeatedly to determine evenness. Who needs %
?
2. Quirky Sorting Function
function sort(arr) {
for (let i = 0; i < arr.length; i++) {
setTimeout(() => console.log(arr[i]), arr[i]);
}
}
Instead of actually sorting, it leverages setTimeout
to "sort" by delay! Time to think outside the box.
3. Convert a Number to a String Without toString()
function numberToString(num) {
return num + '';
}
Elegantly simple! Just concatenate an empty string.
4. Find Maximum Using Math.min
function findMax(...nums) {
return -Math.min(...nums.map(n => -n));
}
A clever inversion of Math.min
to find the maximum.
5. Infinite Loop Counter
function infiniteCounter() {
let i = 0;
setInterval(() => console.log(i++), 1000);
}
Why count to 10 when you can count forever?
6. Flatten an Array Without Loops
function flatten(arr) {
return arr.flat(Infinity);
}
A short and sweet way to handle deeply nested arrays.
7. Reverse a String Using reduce
function reverseString(str) {
return str.split('').reduce((rev, char) => char + rev, '');
}
A clever use of reduce
to reverse a string.
8. Generate a Random Boolean
function randomBoolean() {
return Math.random() < 0.5;
}
Sometimes you just need a 50/50 chance.
9. Simulate a Coin Toss
function coinToss() {
return Math.random() < 0.5 ? 'Heads' : 'Tails';
}
Heads or tails? Let JavaScript decide.
10. Check for Palindrome Without Loops
function isPalindrome(str) {
return str === str.split('').reverse().join('');
}
One-liner to check for palindromes.
11. Sleep Function Without Promises
function sleep(ms) {
const start = Date.now();
while (Date.now() - start < ms) {}
}
Why not block the entire thread to "sleep"?
12. Check If a Value Is NaN Without isNaN()
function isItNaN(value) {
return value !== value;
}
NaN
is the only JavaScript value that is not equal to itself.
13. FizzBuzz in One Line
function fizzBuzz(n) {
return Array.from({ length: n }, (_, i) => (++i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i);
}
FizzBuzz in style.
14. Generate an Array of Random Numbers
function randomArray(length) {
return Array.from({ length }, () => Math.random());
}
A quick way to create an array of random numbers.
15. Shuffle an Array
function shuffleArray(arr) {
return arr.sort(() => Math.random() - 0.5);
}
Randomize your array elements with ease.
16. Get Unique Values from an Array
function uniqueValues(arr) {
return [...new Set(arr)];
}
Set
is your friend for deduplication.
17. Multiply Without *
function multiply(a, b) {
return Array.from({ length: b }, () => a).reduce((sum, num) => sum + num, 0);
}
A wild way to perform multiplication.
18. Find Factorial Recursively
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
A recursive approach to calculate factorial.
19. Simulate a Delay with Promises
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
A more modern way to "pause" execution.
20. Generate Fibonacci Sequence
function fibonacci(n) {
return Array.from({ length: n }).reduce((acc, _, i) => {
acc.push(i < 2 ? i : acc[i - 1] + acc[i - 2]);
return acc;
}, []);
}
Elegant and functional.
21. Generate a Random Hex Color
function randomHexColor() {
return `#${Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, '0')}`;
}
Randomize your color palette in style.
Conclusion
These quirky JavaScript functions show how creativity can lead to fascinating (and sometimes baffling) solutions. While they may not always be the most practical, they push the boundaries of whatβs possible with code. Which one is your favorite? Share your own quirky functions below!
Top comments (0)