JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
19: String Methods: padStart, padEnd, trim
Imagine you're designing a digital scoreboard for a basketball game. If the score is 5, you don't want the screen to just show a lonely "5" floating in the middle of a wide slot. You want it to look professional, so you show "05". Now, imagine you're also printing a physical receipt. You want the item name on the left and the price on the right, with a neat row of dots filling the gap between them. Finally, imagine a user signing up for your app, but they're clumsy and accidentally hit the spacebar three times before typing their email. If you save that string exactly as it is, your database is going to have a bad time.
In JavaScript, we handle these specific "visual cleanup" and "data sanitization" tasks using padStart, padEnd, and trim. They aren't about changing the meaning of the data, but rather how that data is framed or polished.
Fixing the 'Single Digit' Problem with padStart
padStart() is your go-to when you need a string to reach a minimum length by adding characters to the beginning. It takes two arguments: the target length you want, and the character you want to use for filling.
const minutes = "5";
const formattedMinutes = minutes.padStart(2, "0");
console.log(formattedMinutes); // "05"
const hour = "9";
const formattedHour = hour.padStart(2, "0");
console.log(formattedHour); // "09"
I've found that this is incredibly useful for dates and timestamps. If the string is already equal to or longer than the target length, JavaScript just leaves it alone. It won't chop your string off; it only adds if there's room.
Creating Visual Columns with padEnd
padEnd() is the mirror image of padStart. It adds characters to the end of the string. This is less about "formatting numbers" and more about "aligning text."
Think about a menu or a price list. You want the names of the items to take up a consistent amount of space so the prices line up vertically.
const item1 = "Coffee";
const item2 = "Croissant";
const filler = ".";
console.log(item1.padEnd(15, filler) + "$3.50");
console.log(item2.padEnd(15, filler) + "$4.25");
// Output:
// Coffee.........$3.50
// Croissant......$4.25
By forcing every item string to be 15 characters long, the prices are now perfectly aligned. It's a simple trick, but it makes a huge difference in CLI (Command Line Interface) tools.
Cleaning Up Messy User Input with trim
Now, let's talk about trim(). Unlike the padding methods, trim() doesn't take any arguments. Its only job is to look at the start and end of a string and rip away any whitespace—spaces, tabs, or line breaks.
I cannot stress enough how important this is when dealing with HTML form inputs. Users are unpredictable. They copy-paste emails with a trailing space, or they accidentally indent their username. If you compare "admin" to "admin ", JavaScript will tell you they aren't the same, which leads to those frustrating "Invalid Password" errors when the password is actually correct.
const userInput = " dev_master_99 ";
const cleanInput = userInput.trim();
console.log(`"${userInput}"`); // " dev_master_99 "
console.log(`"${cleanInput}"`); // "dev_master_99"
Just a heads-up: trim() only handles the edges. If the user puts a space in the middle of their username, trim() won't touch it. For that, you'd need something more powerful like a Regular Expression, but for 90% of input cleanup, trim() is all you need.
📋 Practical Task
Building a Digital Transaction Receipt
You are building a small script to format a transaction receipt for a coffee shop. You have been given a raw set of data, but it's messy and unaligned.
Your goals:
- Clean up the
customerNameby removing any accidental leading or trailing whitespace. - Ensure the
transactionID(which is a string) is exactly 6 characters long, padding the start with zeros if it's too short. - Format the
itemandpriceso that the item name is padded to 20 characters using a period (.) as the filler, ensuring the prices line up perfectly.
const rawCustomerName = " Jane Doe ";
const rawTransactionID = "42";
const item = "Caramel Macchiato";
const price = "$5.50";
// Your code here:
// 1. Trim the customer name
// 2. Pad the transaction ID
// 3. Pad the item name
// Expected Output:
// Customer: Jane Doe
// ID: 000042
// Caramel Macchiato....$5.50
There are no comments for now.