Member-only story
Some JavaScript Tips To Make You Look More Like a Master
Good code is refreshing and easy to understand, and also gives you a sense of accomplishment.

As programmers, we need a lot of writing skills to write code as well. A good code can be refreshing, easy to understand, comfortable and natural, and at the same time make you feel full of achievement. So, I’ve put together some JS development tips that I’ve used to help you write code that is refreshing, easy to understand, and comfortable.
String Tips
- Generate random ID
const RandomId = len => Math.random().toString(36).substr(3, len);
const id = RandomId(10);
console.log(id); //l5fy28zzi5
2. Formatting Money
const ThousandNum = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
const money = ThousandNum(2022112213);
console.log(money); //2,022,112,213
3. Generate star ratings
const StartScore = rate => "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);
const start = StartScore(3);
console.log(start); // ★★★☆☆
4. Generate random HEX color values
const RandomColor = () => "#" + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0");
const color = RandomColor();
console.log(color); // #bd1c55
5. Operation URL query parameters
const params = new URLSearchParams(location.search.replace(/\?/ig, ""));
// location.search = "?name=young&sex=male"
params.has("young"); // true
params.get("sex"); // "male"
Number Tips
- rounding
Math.floor()
for positive numbers,Math.ceil()
for negative numbers.
const num1 = ~~2.69;
const num2 = 3.69 | 0;
const num3 = 5.69 >> 0;
console.log(num1); // 2
console.log(num2); // 3
console.log(num3); // 5
2. Complementary zero
const FillZero = (num, len) => num.toString().padStart(len, "0");
const num = FillZero(187, 6);
console.log(num); //000187