JavaScript One-Liners for Senior Frontend Engineers
JavaScript is a versatile language, and knowing these one-liners can make you look like a pro. Let’s dive into some concise yet powerful code snippets that will enhance your JavaScript knowledge.
These one-liners are a sure way of showing of your expertise to your co-workers in no time. Let’s get started!
1. Generate a Random String
Creating a random string is a common task, especially when you need unique identifiers. You can achieve this with Math.random()
, converting the number to base-36, and slicing off the leading "0."
const randomString = () => Math.random().toString(36).slice(2);
randomString() // gl1qtdego0q
randomString() // f4qixv40moc
randomString() // eielv1pm3ju
Documentation for Math.random()
2. Escape HTML Special Characters
To prevent cross-site scripting (XSS) attacks, it’s essential to escape HTML special characters in strings. This function replaces…