Formatting Strings with JavaScript Padstart and Padend Functions

preview_player
Показать описание
If you need to format a string to be a certain length, the built-in JavaScript padStart and padEnd functions will do just that. With JavaScript padStart and padEnd, you pass two parameters: one for the desired length of the padded string, and one for the character you want to pad your string with.

COURSES
Learn everything you need to know about Visual Studio Code!!

Рекомендации по теме
Комментарии
Автор

That is the first time I have heard about these methods, but I can see how they would be handy. Thanks for the great tutorial!

leecastelani
Автор

Great Tutorial James, I never knew these methods actually existed! With great humility, the only thing I might want to refactor is the homemade padding & dollar & cents examples

// homemade "Padding" methods
const padAtStart = (inputStr, length, char) => {
const padding = Math.abs(length - inputStr.length);
return
};

const padAtEnd = (inputStr, length, char) => {
const padding = Math.abs(length - inputStr.length);
return
};

console.log(padAtStart('James', 10, '1')); //
console.log(padAtEnd('James', 10, '1')); //

// dollar & cents example
const money = (dollar, cent) => {
const cents = cent.toString();
const doubleCents = `$${dollar}.${cents}`;
const singleCents = `$${dollar}.${cents.padStart('2', '0')}`;
return cents.length === 1 ? singleCents : doubleCents;
};

console.log(money(20, 1)); // $20.01
console.log(money(20, 11)); // $20.11


Not trying to showoff, I’m a newbie here, but the code seems more readable this way for some reason.

noirsociety
Автор

Weldone James, it's my first time hearing about the functions.

senaholiz
Автор

James, your tutorials are always great.

moethetkoko
Автор

Underrated video,

Is there any other inbuilt method to force pad or something?

like if a string is of length 12 and I want it to be of length 10, how will I go around making it actually force the length to be 10?

dhruvinpurohit
Автор

Dang, I got a really wrong idea about it's use case from reading MDN lol. I thought I could use it to slap html tags on the front and back lol. Hilariously wrong....

arshadsiddiqui
Автор

If I have an variable in array with this padding functionality and I just want to update variable with counter for every array item which includes padding ...how to do that.

jayakrishnalakkoju
Автор

Why do computer guys have a need to buy weird office chairs ?

femaledeer
visit shbcf.ru