12 Life-Saving JavaScript One-Liners to Boost Up Your Productivity
DhiWise
August 8, 2022
Author
DhiWise
{
August 8, 2022
}
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.
When you want to create a temporary unique id for authentication or anything else, here is how you can make it.
const randomString = () => Math.random().toString(36).slice(2);
console.log(randomString()); // could be anything!!!
3. Capitalizing a string
You can use the code in the snippet to capitalize input.
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
console.log(capitalize('follow for more')); // Follow For More
4. Calculating the number of days between two dates
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.
console.log(
truncateStringMiddle(
'A long story goes here but then eventually ends!', // string
25, // total size needed
13, // chars to keep from start
17, // chars to keep from end
),
); // A long story ... eventually ends!
9. Truncate string at the end
It's super easy to truncate strings.
const truncateString = (string, length) => {
return string.length < length ? string : `${string.slice(0, length - 3)}...`;
};
console.log(truncateString('Truncate the string because, its too long', 30));
// Truncate the string because...
10. Wait for certain time before execution
In asynchronous programming, if you want to wait for a certain amount of time, here is the way you can do that.
const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
const test = async () => {await wait(1000)
console.log("waited for 1 second.")
}
test()
// the output will be generated after 1000 milliseconds.
11. Convert string to a URL slug
Convert your String to URL using Regular expression that will remove the special characters and add “-” in between the words.
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!
Summing up:
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.