- n – the [[mdn>Web/JavaScript/Reference/Global_Objects/Number|Number]] of characters that the target stringshould take.
- pad – A single-character [[mdn>Web/JavaScript/Reference/Global_Objects/String|String]] that should be used to fill up the target string, if the source string was shorter than n (optional).
Notes:
* The pad parameter, if specified, must be a //single-character// string. If this parameter is longer than one character, the method may return unexpected results.
===== Examples =====
Simple examples:
"Lorem ipsum".right(0); // result: ""
"Lorem ipsum".right(1); // result: "m"
"Lorem ipsum".right(5); // result: "ipsum"
"Lorem ipsum".right(15); // result: "Lorem ipsum"
The last example shows that if the source string is shorter than the number given, this will just return the entire source string, but no more.
That is, unless you also specify a padding character:
"Lorem ipsum".right(15, "-"); // result: "----Lorem ipsum"
A good use-case for the padding option is the formatting of hexadecimal numbers:
(12).toString(16).right(4, "0"); // result: "000c" (= decimal 12)
===== Relevant JavaScript methods =====
* [[mdn>Web/JavaScript/Reference/Global_Objects/String/slice|String.prototype.slice()]] on MDN.
* [[mdn>Web/JavaScript/Reference/Global_Objects/String/substring|String.prototype.substring()]] on MDN.