• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Add characters to the end of a string if it’s less than a certain length

Add characters to the end of a string if it’s less than a certain length

Add characters to the end of a string if it’s less than a certain length

Accepts two arguments: the length the string should be, and what characters to add if it’s not that length. The characters to use is optional, and defaults to a space ().

// Add a leading zero for hours below 10
var minutes0 = '0';
var minutes12 = '12';

// returns "00"
minutes0.padEnd(2, '0');

// returns "12"
minutes12.padEnd(2, '0');

Source

https://vanillajstoolkit.com/reference/strings/string-padend/

Miscellaneous

Related Snippets:

  • Insert an HTML string after the end of the specified element
  • Return every nth element in an array
  • Prevent the default action of an event
  • Determine if a string contains a substring

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us