Flexible application of JS(JavaScript) development skills enhances development efficiency🚀 and happiness🥰.

aifou
5 min readMar 15, 2024

1.“Compare the time.”

“Single-digit numbers in time should be padded with a zero.”

const time1 = "2019-02-14 21:00:00";
const time2 = "2019-05-01 09:00:00";
const overtime = time1 > time2;
// overtime => false

2.“Format the money.”

const ThousandNum = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
const money = ThousandNum(20190214);
// money => "20,190,214"

3.“Generate a random ID.”

const RandomId = len => Math.random().toString(36).substr(3, len);
const id = RandomId(10);
// id => "jg7zpgiqva"

4.“Generate a random HEX color value.”

const RandomColor = () => "#" + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0");
const color = RandomColor();
// color => "#f03665"

5.“Generate star ratings.”

const StartScore = rate => "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);
const start = StartScore(3);
// start => "★★★"

6.Operating 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"

7.Round down

Math.floor() for positive numbers, Math.ceil() for…

--

--