I'm always thinking how can I do something but then I ask myself how can I do it better… Is not only just to challenge me but to create more efficient apps. All the time that you can save is translated to productivity and that make us better people. Now I wanna share with you some JavaScript snippets.
Today number
Returning the number of the day in the year
The day that I was writing this article was 12th of January, so it does return 12.
Fifty Fifty
Returning randomly true or false with the same 50% of probability
Dynamic call
Calling dynamically a function inside an array
Getting form
It's quite easy to get the data from our form inputs
Serializing data
JSON data
UUID
Generate a unique universal identifier
Device type
You can improve your website responsivity by detecting the device of the user
Capitalize
Capitalizing the first letter from string
Decapitalize
As well as we can capitalize letters, we can revert it like this
Digitize
Converts a number into an array of digits.
Escape HTML
Sometimes we want to store HTML content into a database but to achieve this it's vital to escape the HTML tags.
Unescape HTML
Well, if we want to get some day again the escaped tags it would be necessary to unescape them, isn't it?
Strip HTML
Instead of still using the HTML strings maybe you only want the content inside so lets get rid the tags
Cleaning string
Remove the non ASCII characters from strings
Native type
Returns the native type of any value in JavaScript
Currency
Formatting numbers as money quantities is more easy than you think
Isn't perfect but is a great approach to get formatted our currencies.
Remove with logic
With this snippet we are improving directly the Array prototype by adding a new method called "Remove", even when I've already talked about removing keys from arrays in this story, this is better because in this way we can remove keys that match with the function that we pass as parameter.
JavaScript Tricks in real life
As almost all my stories, you can see alive all this snippets in this Codepen
https://codepen.io/adrian-legaspi/pen/rNavRLx
Conclusion
The only limitation when we are creating is ourselves. Give a try to this snippets and why not?, create new ones and share them. The more knowledge you share, the more that all of us will growth together, keep coding nice.
References:
30 seconds of code → https://www.30secondsofcode.org/js/
KamiLelonek → https://gist.github.com/KamilLelonek/a4b8a27cbfb766b0b5ac
Myself → https://twitter.com/ImLuyou