How can you code less and achieve more?
There can be multiple ways, but here we are going to cover the unique way that most developers love to use.
**“JavaScript One-Liner!”**🤘
JavaScript is a wonderful language that lets you do amazing things from developing frontend to backend and handling complex APIs. But beyond that, it also facilitates creating awesome stuff just using a single line of code.🥰
Don’t you think it's always cool to learn new things?
So get familiar with the 12 JavaScript OneLiners to boost up your JS app development productivity.
Let's get started! 🚀
The easiest way to check variables in an array.
1 const isArray = (arr) => Array.isArray(arr); 2 console.log(isArray([1, 2, 3])); 3 // true 4
1 console.log(isArray({ name: 'Sara' })); 2 // false 3 console.log(isArray('Hello World')); 4 // false 5
When you want to create a temporary unique id for authentication or anything else, here is how you can make it.
1 const randomString = () => Math.random().toString(36).slice(2); 2 console.log(randomString()); // could be anything!!!
You can use the code in the snippet to capitalize input.
1 const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1); 2 console.log(capitalize('follow for more')); // Follow For More 3
To calculate the number of days between two dates, find the absolute between two dates and then divide it by 86400000 (milliseconds in a single day) at the end of the day we will round the result and return the output.
1 const daysDiff = (date, date2) => Math.ceil(Math.abs(date - date2) / 86400000); 2 console.log(daysDiff(new Date('2022-05-10'), new Date('2022-11-25'))); // 199
Shuffling two arrays using sort and random methods.
1 const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random()); 2 3 console.log(shuffleArray([1, 2, 3, 4])); // Result: [ 1, 4, 3, 2 ] 4
To reverse a string you can use the split, reverse and join method.
1 const reverse = str => str.split('').reverse().join(''); 2 3 reverse('Hello World'); // Result: 'dlroW olleH' 4
You can merge an array using the “concat” method, the spread [“...”]operator and finally, we can use the Set object to remove any duplicate.
1 // Merge but don't remove the duplicates 2 const merge = (a, b) => a.concat(b); 3 // Or 4 const merge = (a, b) => [...a, ...b]; 5 // Merge and remove the duplicates 6 const merge = [...new Set(a.concat(b))]; 7 // Or 8 const merge = [...new Set([...a, ...b])];
What if you want to truncate the string from the Middle?
The function will take the string as the first parameter, length as the second, and start and end as the third and fourth parameters.
1 const truncateStringMiddle = (string, length, start, end) => { 2 return `${string.slice(0, start)}...${string.slice(string.length - end)}`; 3 }; 4
1 console.log( 2 truncateStringMiddle( 3 'A long story goes here but then eventually ends!', // string 4 25, // total size needed 5 13, // chars to keep from start 6 17, // chars to keep from end 7 ), 8 ); // A long story ... eventually ends! 9
It's super easy to truncate strings.
1 const truncateString = (string, length) => { 2 return string.length < length ? string : `${string.slice(0, length - 3)}...`; 3 }; 4 console.log(truncateString('Truncate the string because, its too long', 30)); 5 // Truncate the string because... 6
In asynchronous programming, if you want to wait for a certain amount of time, here is the way you can do that.
1 const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds)); 2 const test = async () => {await wait(1000) 3 console.log("waited for 1 second.") 4 } 5 test() 6 // the output will be generated after 1000 milliseconds. 7
Convert your String to URL using Regular expression that will remove the special characters and add “-” in between the words.
1 const slugify = string => string.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, ''); // Example 2 console.log(slugify('Lifesaving JavaScript OneLiners')); 3 // Result:lifesaving-javascript-oneliners 4
Nowadays every website uses cookies. You can get the value of a specific cookie that you want to know using the following function.
1 const cookie = name => `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift(); 2 console.log(cookie('_ga')); // Result: GA1.1.1556710793.1646367473 3
There is no doubt JavaScript OneLine improves developers' productivity, but as we know it's not the only way to achieve it.
Let's find out how we can help you to improve your full-stack JavaScript app development efficiency with the revolutionary app development platform.
Whether you are a frontend or backend JavaScript developer DhiWise offers you everything to make your app development a breeze.
Here are some of the functionalities of DhiWise that you must look into.
DhiWise Figma to Code(React.js, Kotlin, Flutter, and Swift).
Supports Front end technologies such as React.js, and Flutter.
Support backend Technologies such as Node.js and Laravel for PHP.
Supports full-stack development technologies like JavaScript, Swift, and Kotlin.
Generates clean Code, simplifying code maintenance and improving scalability.
Logic builder to create app-specific workflows from scratch.
Integrated with Firebase and Superbase.
The platform offers a complete code view and flexibility to make changes at any time during development and once the app is built.
Simplified team collaboration, code sharing, and management.
If you are still not assured about DhiWise capabilities, just try it, we have made sure that you will always find it super easy and fast to code.
So enjoy coding!
That's all, I hope you enjoyed the article. Now you know about the 12 javaScript OneLiner + functionalities of DhiWise. Don’t forget to check out our other medium articles here.
Have a nice day!