Skip to content

Commit b017592

Browse files
Added pad.Start() and padEnd()
Added a code snippet that helps to add padding at the start or end of the string.
1 parent 9021722 commit b017592

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

ECMASCript 2021/padding.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// What's the use of `padStart()` and `padEnd()` in JavaScript?
2+
//
3+
// String padding is often needed to ensure consistent formatting.
4+
// Common examples in ServiceNow:
5+
// - Padding numeric IDs with leading zeros (e.g., "INC000045")
6+
// - Making export data columns align neatly
7+
// - Adding placeholders to strings for integration formatting
8+
//
9+
// padStart(targetLength, padString)
10+
// → Pads a string from the start until it reaches the target length
11+
//
12+
// padEnd(targetLength, padString)
13+
// → Pads a string from the end until it reaches the target length
14+
//
15+
// Example:
16+
// "1234".padStart(10, "x"); // → "xxxxxx1234"
17+
// "1234".padEnd(10, "y"); // → "1234yyyyyy"
18+
19+
(function executeRule(current, previous /* null when async */) {
20+
21+
let num = "1234";
22+
23+
// Pad the start with "x"
24+
let padStartExample = num.padStart(10, "x");
25+
gs.info("padStart result: " + padStartExample); // "xxxxxx1234"
26+
27+
// Pad the end with "y"
28+
let padEndExample = num.padEnd(10, "y");
29+
gs.info("padEnd result: " + padEndExample); // "1234yyyyyy"
30+
31+
// ServiceNow use case: format Incident number
32+
let incidentId = "45"; // Example numeric part
33+
let formattedIncident = "INC" + incidentId.padStart(6, "0");
34+
gs.info("Formatted Incident Number: " + formattedIncident); // "INC000045"
35+
36+
})(current, previous);

0 commit comments

Comments
 (0)