Member-only story

Some JavaScript Tips To Make You Look More Like a Master

Maxwell
Level Up Coding
Published in
4 min readNov 23, 2022

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

  1. 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

  1. 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

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Written by Maxwell

A front-end enthusiast, a development engineer willing to learn more about development techniques and collaborate to build better software.

Write a response

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 ful...

so beautifully said :)

--

Another way to clone objects is structuredClone:

https://developer.mozilla.org/en-US/docs/Web/API/structuredClone

--

Very good , very helpful its programmer daily used

--