25 JavaScript One-Liners ที่ช่วยให้คุณทำงานสะดวกขึ้น
09-ส.ค.-23
คัมภีร์เทพ IT
ในการทำงาน ทุกคนล้วนต้องการที่จะทำงานให้เสร็จด้วยความสะดวกและรวดเร็ว แต่ก็ยังคงรักษาไว้ซึ่งประสิทธิภาพที่ดีด้วยเช่นกัน ไม่เว้นแม้แต่ Developer สำหรับคนทำงานกับ JavaScript วันนี้เรามาดู 25 JavaScript One-Liners ที่ช่วยให้คุณทำงานสะดวกขึ้น
1. Copy content to the clipboard
เพื่อปรับปรุง User Experience ของ Website เรามักจะต้อง Copy Content ไปยัง Clipboard เพื่อให้ User สามารถPaste ลงในส่วนที่ต้องการได้
2. Get the mouse selection
คุณเคยเจอสถานการณ์แบบนี้มาก่อนบ้างไหม?
เราจำเป็นต้องได้รับ Content ที่ User ทำการ Select
3. Shuffle an array
สิ่งนี้เป็นเรื่องธรรมดามากใน Lottery Programs แต่ก็ไม่ได้ถือว่า เป็นการสุ่มอย่างแท้จริงนัก
4. Convert rgba to hexadecimal
เราสามารถแปลงค่าสี rgba เป็น hexadecimal ได้
5. Convert hexadecimal to rgba
และเราก็สามารถแปลงค่าสี hexadecimal เป็น rgba ได้เช่นกัน
6. Get the average of multiple numbers
การใช้ Reduce จะทำให้เราได้รับค่าเฉลี่ยของชุด Arrays อย่างสะดวก
7. Check if a number is even or odd
คุณจะบอกได้อย่างไรว่า ตัวเลขใดที่เป็น เลขคี่ หรือ เลขคู่?
8. Deduplicate elements in an array
หากต้องการลบ Elements ที่ซ้ำกันใน Array การใช้ Set จะทำให้มันง่ายขึ้น
9. Check if an object is an empty object
มันง่ายที่จะตัดสินว่า Object นั้น Empty หรือไม่?
10. Reverse a string
11. Calculate the interval between two dates
12. Find the day of the year in which the date falls
สมมติว่าวันนี้คือ วันที่ 23 มิถุนายน 2023 แล้วมันเป็นวันที่อยู่ในลำดับที่เท่าไหร่ของปีนี้ล่ะ?
13. Capitalize the first letter of the string
14. Generate a random string of specified length
15. Get a random integer between two integers
16. Specified digits rounded
17. Clear all cookies
18. Detect if it is dark mode
19. Scroll to the top of the page
20. Determine if it is an Apple device
21. Random Boolean values
22. Get the type of the variable
23. Determine if the current tab is active or not
24. Check if an element is focused
25. Random IP
ที่มา: https://javascript.plainenglish.io/
รับตำแหน่งงานไอทีใหม่ๆ ด้วยบริการ IT Job Alert
อัพเดทบทความจากคนวงในสายไอทีทาง LINE ก่อนใคร
อย่าลืมแอดไลน์ @techstarth เป็นเพื่อนนะคะ
บทความล่าสุด