JavaScript One Liners 40+ 🔥
Published: 2023-05-19 16:44:13

👋 Assalamu alaikum ! Comment your Best One-liner Ts/Js code. 🤲

1. Get a random boolean
This function will return a boolean (true or false) using Math.random() method. It’s a 50/50 chance to get either true or false.


2. Check if the provided date is a weekday or Weekend

3. Check if a number is even or odd

4. Remove all duplicate values in an array

5. Check if a variable is an array
A clean and easy way to check if a variable is an array.


6. Generate a random number between two numbers
This will take two numbers as params and will generate a random number between those two numbers!


7. Generate a random string (unique id?)

8. Swapping Two Variables || Destructuring
Destructuring assignment that swaps the variables values


9. Calculate number of days between two dates
To calculate the days between two dates, we first find the absolute between two dates and then divide it with 24 * 60 * 60 * 1000 = 86400000 which is equal to milliseconds in a single day, and at the end, we round the result and return it.


🔥 You can use Math.round or Math.floor instead of Math.ceil.

10. Different ways of merging multiple arrays

11. Get the actual type of javascript primitives

12. Truncate

13. Capitalizing a string

14. Check if the current tab is in view/focus
This simple helper method returns true or false depending on if a tab is in view/focus


15. Reverse a string

16. Check if an element is currently in focus
We can check if an element is currently in focus using the document.activeElement property.


17. Check if the current user has touch events supported

18. Scroll to top of the page

19. Get average value of arguments
We can use the reduce method to get the average value of the arguments.


20. Convert Fahrenheit / Celsius
Dealing with temperatures can be confusing at times. These 2 functions will help you convert Fahrenheit to Celsius and the other way around.


21. Get query parameters from the URL
To obtain query parameters, we must first divide the URL at “?” and then replace “=” with “:” and “&” with “,”.


22. Clipboard API
To copy a text, we can use JavaScript navigator.


To paste text:


23. Get Value of a brower Cookie
Retrieve the value of a cookie by accessing with document.cookie


24. Check if Date is Valid

25. Find which is the day by a given date in year.

26. Clear All Cookies
You can easily clear all cookies stored in a web page by accessing the cookie using document.cookie and clearing it.


27. Check if array is empty && Object Is Empty

28. Get Selected Text
Get the text the user has select using inbuilt getSelection property.


29. Detect Dark Mode

30. Shuffle an Array
Shuffling an array is super easy with sort and random methods.


31. Generate Random Hex

32. Convert RGB to Hex

33. Get Min & max value of an array

34. Reload the current page

35. Check if a string consists of a repeated character sequence

36. Convert a letter to associate emoji

37. Calculate the angle of a line defined by two points

38. Convert radians to degrees && degrees to radians

39. Wait for an amount of time

40. Create an object from the pairs of key and value

41. Get union of arrays

42. Partition an array based on a condition

43. Remove falsy values from array

That’s all Aliens! 👽
Hope you found this helpful, see you in the next one 😉


Mukhriddin WEB
Assalomu alaykum va rohmatullohi va barakatuhu!!! Alloh har bir ishimizni xayrli qilsin !!!
t.me

https://instagram.com/mukhriddin_dev

MUKHRIDDIN KHODIEV
Full stack Developer Mukhriddin Khodiev 💻 😎 👨‍🎓 University of Information Technologies. Student ✅ Founder of SERIUS…
www.youtube.com

mukhriddin-dev - Overview
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
github.com


Reactjs