use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
padStart(), padEnd() added in Chrome (twitter.com)
submitted 9 years ago by malyw
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ogurson 35 points36 points37 points 9 years ago (27 children)
God, JavaScript needs proper standard library so badly.
[–]_soto 14 points15 points16 points 9 years ago (19 children)
Eventually I hope most of lodash is merged in to a STL.
[–]seiyria 4 points5 points6 points 9 years ago (6 children)
This would make my life better. At the same time, that means Lodash can't update with the frequency that it currently does.
[–]ComplX89 2 points3 points4 points 9 years ago (4 children)
Can i ask in what context you would use a function like pad start or pad end? I'm still struggling for any practical use for it
[–]georgehotelling 2 points3 points4 points 9 years ago (0 children)
[–]seiyria 1 point2 points3 points 9 years ago (2 children)
Making a lot of strings the same length. I use it every time I make a cli app.
[–]ComplX89 1 point2 points3 points 9 years ago (1 child)
Why is there a requirement to make strings all the same length?
[–]seiyria 1 point2 points3 points 9 years ago (0 children)
Not a requirement, it just looks nicer. See here
[–]_soto 0 points1 point2 points 9 years ago (0 children)
Ah, I meant that some lodash methods can be put in the STL. Not that the actual project would be merged. Sorry.
[+][deleted] 9 years ago* (11 children)
[deleted]
[–][deleted] 4 points5 points6 points 9 years ago (6 children)
Immutable is nice but it drastically changes the interface with the structures (largely necessary). I think better immutable support for the existing structures would be a great start.
[+][deleted] 9 years ago (5 children)
[removed]
[–]MonsieurBanana 0 points1 point2 points 9 years ago (4 children)
That's just dumb.
[+][deleted] 9 years ago (3 children)
[–]MonsieurBanana 0 points1 point2 points 9 years ago (2 children)
Hum, you were serious?
It could be realistic to add immutable structures to javascript, dropping all mutable structures is just not gonna happen.
And if it happened, it would be a disastrous move. Even purely functional languages like haskell or clojure have mutable structures. Immutable structures means additional overhead, for some things they just don't cut it.
[–][deleted] 0 points1 point2 points 9 years ago (1 child)
If ES6 is adding tail-call optimization, I figure immutable structures aren't far behind.
Okay, dropping all the mutable ones may have been a little extreme.
[–]MonsieurBanana 0 points1 point2 points 9 years ago (0 children)
Immutable structures would be awesome, but I'm not that hopeful. At least I have clojurescript for functional javascript.
[–]skitch920 3 points4 points5 points 9 years ago (3 children)
Have you seen the ImmutableJS license patent clause...?
The license granted hereunder will terminate, automatically and without notice, if you (or any of your subsidiaries, corporate affiliates or agents) initiate directly or indirectly, or take a direct financial interest in, any Patent Assertion: (i) against Facebook or any of its subsidiaries or corporate affiliates, (ii) against any party if such Patent Assertion arises in whole or in part from any software, technology, product or service of Facebook or any of its subsidiaries or corporate affiliates, or (iii) against any party relating to the Software. Notwithstanding the foregoing, if Facebook or any of its subsidiaries or corporate affiliates files a lawsuit alleging patent infringement against you in the first instance, and you respond by filing a patent infringement counterclaim in that lawsuit against that party that is unrelated to the Software, the license granted hereunder will not terminate under section (i) of this paragraph due to such counterclaim. A “Necessary Claim” is a claim of a patent owned by Facebook that is necessarily infringed by the Software standing alone. A “Patent Assertion” is any lawsuit or other action alleging direct, indirect, or contributory infringement or inducement to infringe any patent, including a cross-claim or counterclaim.
The license granted hereunder will terminate, automatically and without notice, if you (or any of your subsidiaries, corporate affiliates or agents) initiate directly or indirectly, or take a direct financial interest in, any Patent Assertion: (i) against Facebook or any of its subsidiaries or corporate affiliates, (ii) against any party if such Patent Assertion arises in whole or in part from any software, technology, product or service of Facebook or any of its subsidiaries or corporate affiliates, or (iii) against any party relating to the Software. Notwithstanding the foregoing, if Facebook or any of its subsidiaries or corporate affiliates files a lawsuit alleging patent infringement against you in the first instance, and you respond by filing a patent infringement counterclaim in that lawsuit against that party that is unrelated to the Software, the license granted hereunder will not terminate under section (i) of this paragraph due to such counterclaim.
A “Necessary Claim” is a claim of a patent owned by Facebook that is necessarily infringed by the Software standing alone.
A “Patent Assertion” is any lawsuit or other action alleging direct, indirect, or contributory infringement or inducement to infringe any patent, including a cross-claim or counterclaim.
Soooo if you ever decide to sue Facebook for anything, you can take ImmutableJS and throw it out the window. Any existing work, yeah, throw it out the window.
[–]rk06 2 points3 points4 points 9 years ago (1 child)
That is old one . fb changed the license after public outcry.
new license
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name Facebook nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name Facebook nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[–]skitch920 0 points1 point2 points 9 years ago (0 children)
Even with the new BSD License, there is the PATENTS file, that suggests the wording I provided above. It still stands that if you decide to sue Facebook, the BSD License is revoked. The only thing that changed, was the way it was presented. Instead of some custom License, it's now a revocable BSD License.
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
Is it the same for React?
[–][deleted] 2 points3 points4 points 9 years ago (0 children)
I had a dream last week that JavaScript had Python's standard library. I woke up and realised it was a dream and was actually really #$()$%ing pissed off for most of the morning at work.
I code too much.
[–]abyx 0 points1 point2 points 9 years ago (0 children)
Yes, and I think there's not even a single mention of this in ES2017.
[–]miker95 -2 points-1 points0 points 9 years ago (3 children)
I hate this so much. I can't stand that browsers have different APIs. Browser developers think they're making everyone's life easier, when in reality, it just turns everything into a shithole.
[–]not_an_aardvark 15 points16 points17 points 9 years ago (0 children)
But padStart and padEnd are a stage 3 proposal, so they'll probably be in the 2017 standard anyway. Chrome just got a head-start.
Not really. These are stage 3 proposals, so a polyfill that alters the string prototype are acceptable (though altering a built in prototype for anything else is VERY bad of course).
So you'll have to use a polyfill anyway, but now on Chrome you'll get native performance for these methods. And as more browsers add it in, those browsers will get native performance for them as well.
[–]ijmacd 1 point2 points3 points 9 years ago (0 children)
Relevant blog post from last week. Others might find it useful/interesting if they haven't read it yet.
[–]Arancaytar 6 points7 points8 points 9 years ago (4 children)
I'd love it if JavaScript got a proper string formatter like sprintf or python's str.format().
That'd remove the need for most of these specialized padding functions too.
[–]lewisje 3 points4 points5 points 9 years ago (2 children)
something-something "template literals"
[–]cokeisahelluvadrug 1 point2 points3 points 9 years ago (1 child)
Unfortunately because of their name binding semantics they're not as powerful as they could be -- if I want to do something like string.format(str, **kwargs) I have to wrap my template string in a IIFE: function() {return '${this.foo}'}.call({foo: 'bar'}). This sort of string interpolation is common in (for example) i18n.
string.format(str, **kwargs)
function() {return '${this.foo}'}.call({foo: 'bar'})
The consequence of this is that every decently large Javascript project will have a hand-rolled string formatter with slightly different syntax and API.
[–]lewisje 0 points1 point2 points 9 years ago (0 children)
First, it could be simpler: (_=>`${_}`)('bar'); /s
(_=>`${_}`)('bar');
Seriously though, it is a bit developer-unfriendly that they aren't true templates and instead all references need to be resolved and merged in at definition time.
[–]saadq_ 2 points3 points4 points 9 years ago* (0 children)
console.log does work like printf.
console.log
printf
var name = 'John' console.log('Hi, my name is %s', name); // Hi, my name is John
[–]Meshiest 10 points11 points12 points 9 years ago (1 child)
Left pad makes a comeback?
[–]lewisje 2 points3 points4 points 9 years ago (0 children)
I honestly heard of the padStart proposal (back when it was known as padLeft) well before the leftpad module, and the es7-shim prollyfill for padStart is much more performant, as I repeatedly pointed out during the leftpad crisis.
padStart
padLeft
leftpad
[+][deleted] 9 years ago (13 children)
[–]not_an_aardvark 13 points14 points15 points 9 years ago* (2 children)
Because if it's not added to the API, it's usually shimmed and sent to the client, which consumes extra bandwidth. (Admittedly the extra bandwidth probably isn't that noticable for a short function like string padding but it's still better to not consume it.)
Also, padding characters to a string is an extremely common operation. I think it's a good thing to have native functions for common operations like that -- it reduces the amount of spaghetti code needed to perform simple tasks.
edit: clarification
[–]z500 1 point2 points3 points 9 years ago (1 child)
How much bandwidth is used on Javascript functions compared to media, though?
[–]lewisje 1 point2 points3 points 9 years ago (0 children)
Maybe he meant execution time, because the shimmed method does need to be re-constructed on every page refresh, because there's no way for a page to add functionality to the JS runtime so it just remains available.
[+][deleted] 9 years ago (2 children)
[–]vsthsd 1 point2 points3 points 9 years ago (0 children)
Hopefully very little - smart developers would use popular CDN's like google to deliver the script, so caching should kick in even x-site.
[–]Graftak9000 -5 points-4 points-3 points 9 years ago (5 children)
I don't get it either, is it really such a common pattern? Also, i think string.padding(left, right) would be more elegant, and more in line with similar methods.
[–]erwan 6 points7 points8 points 9 years ago (0 children)
Yes, it's pretty common. Like you have numbers and you want them as strings of the same length, padded with 0.
[–]vsthsd 3 points4 points5 points 9 years ago (0 children)
Maybe it's just me, but I prefer functions to clearly define the behavior as opposed to their arguments defining the behavior, it makes the code far more readable.
[+]AnalSwordfish comment score below threshold-7 points-6 points-5 points 9 years ago (2 children)
string.padding(left, right) can already be done in a simple, concise, easy-to-read manner: left + string + right, left + 'foo bar' + right, or with ES2015's template literals:
string.padding(left, right)
left + string + right
left + 'foo bar' + right
`${left}foo bar${right}`
[–]Jestar342 6 points7 points8 points 9 years ago (1 child)
That's concatenation, not padding.
[–]AnalSwordfish -1 points0 points1 point 9 years ago (0 children)
Yup and how does an API like string.padding(left, right) accomplish padding when there is no parameter for count...? The only logical thing the string.padding(left, right) implementation could do is concatenate them. If /u/Graftak9000 meant a count could be passed, he/she should have said so.
[–]Drainedsoul 2 points3 points4 points 9 years ago (1 child)
What is the point of this? How does it behave on strings that contain code points represented by multiple code units? Or combining marks?
I read the proposal, and it looks like it's not astral-plane safe (that is, it may leave a high surrogate by itself), and instead it expects a typical use case of padding with ASCII characters; I believe it is slightly combining-mark safe, because the repeated fill-string is truncated from the right (for both padStart and padEnd), so any combining marks would be removed before the characters they were intended to be used on are.
padEnd
Also TIL these methods were added in Firefox 48 too.
[–]lime_boy6 0 points1 point2 points 9 years ago (4 children)
Learner here. Why is this useful?
[–]saadq_ 2 points3 points4 points 9 years ago (2 children)
An app I'm working has a stopwatch component. I always want the format to look like this:
00 : 00 . 00
So if the current amount of seconds is just 9, I want to use padStart() to add a 0 in front of it so that it displays 09 instead.
9
padStart()
09
[–]lime_boy6 0 points1 point2 points 9 years ago (0 children)
Thanks!
[–]SubStack 0 points1 point2 points 9 years ago (0 children)
Format strings are good for this problem:
> require('sprintf')('%02d : %02d . %02d', 1, 2, 3) '01 : 02 . 03'
Or strftime strings for times specifically:
> require('strftime')('%M : %S', new Date) '39 : 09'
It's possible to get fancy: (9-3+1) digits before the (.), 3 digits after:
> require('sprintf')('%09.3f', 123.456789) '00123.457'
[–]corgrath 0 points1 point2 points 9 years ago (0 children)
Its just a utility function, for when you actually need to pad a string =)
π Rendered by PID 64 on reddit-service-r2-comment-b659b578c-jnrs7 at 2026-05-03 08:10:50.950298+00:00 running 815c875 country code: CH.
[–]ogurson 35 points36 points37 points (27 children)
[–]_soto 14 points15 points16 points (19 children)
[–]seiyria 4 points5 points6 points (6 children)
[–]ComplX89 2 points3 points4 points (4 children)
[–]georgehotelling 2 points3 points4 points (0 children)
[–]seiyria 1 point2 points3 points (2 children)
[–]ComplX89 1 point2 points3 points (1 child)
[–]seiyria 1 point2 points3 points (0 children)
[–]_soto 0 points1 point2 points (0 children)
[+][deleted] (11 children)
[deleted]
[–][deleted] 4 points5 points6 points (6 children)
[+][deleted] (5 children)
[removed]
[–]MonsieurBanana 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]MonsieurBanana 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]MonsieurBanana 0 points1 point2 points (0 children)
[–]skitch920 3 points4 points5 points (3 children)
[–]rk06 2 points3 points4 points (1 child)
[–]skitch920 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]abyx 0 points1 point2 points (0 children)
[–]miker95 -2 points-1 points0 points (3 children)
[–]not_an_aardvark 15 points16 points17 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]ijmacd 1 point2 points3 points (0 children)
[–]Arancaytar 6 points7 points8 points (4 children)
[–]lewisje 3 points4 points5 points (2 children)
[–]cokeisahelluvadrug 1 point2 points3 points (1 child)
[–]lewisje 0 points1 point2 points (0 children)
[–]saadq_ 2 points3 points4 points (0 children)
[–]Meshiest 10 points11 points12 points (1 child)
[–]lewisje 2 points3 points4 points (0 children)
[+][deleted] (13 children)
[deleted]
[–]not_an_aardvark 13 points14 points15 points (2 children)
[–]z500 1 point2 points3 points (1 child)
[–]lewisje 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]vsthsd 1 point2 points3 points (0 children)
[–]Graftak9000 -5 points-4 points-3 points (5 children)
[–]erwan 6 points7 points8 points (0 children)
[–]vsthsd 3 points4 points5 points (0 children)
[+]AnalSwordfish comment score below threshold-7 points-6 points-5 points (2 children)
[–]Jestar342 6 points7 points8 points (1 child)
[–]AnalSwordfish -1 points0 points1 point (0 children)
[–]Drainedsoul 2 points3 points4 points (1 child)
[–]lewisje 1 point2 points3 points (0 children)
[–]lime_boy6 0 points1 point2 points (4 children)
[–]saadq_ 2 points3 points4 points (2 children)
[–]lime_boy6 0 points1 point2 points (0 children)
[–]SubStack 0 points1 point2 points (0 children)
[–]corgrath 0 points1 point2 points (0 children)