Timeline for answer to Does JavaScript have a method like "range()" to generate a range within the supplied bounds? by Kristjan Liiva
Current License: CC BY-SA 4.0
Post Revisions
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 11, 2025 at 5:56 | comment | added | forresthopkinsa | @Asker Javascript doesn't have the luxury of forward-incompatible syntax that other languages do. Rust has made a tremendous number of breaking changes in the course of its evolution. | |
| Aug 17, 2023 at 16:28 | comment | added | Asker | As an aside, it is incredible that a language as high-level and popular as JS still lacks a straightforward range or [:] syntax in 2023. I can fathom no reason for it other than layers upon layers of bureaucratic red tape. Perhaps someone should pressure the ES committee to add such a feature? Some nice new languages, such as Rust, have no problem adding syntactic sugar when it would help, even despite those languages being much more systematic and "closer to the bare metal" than JS. | |
| Oct 9, 2021 at 22:00 | comment | added | Robert Monfera |
The Array.from form could eventually be faster than spread etc. for the original use case on the top (while the spreading version of the answer here doesn't generate what the OP asked). The reason for why Array.from could be faster is that the spec doesn't require the materialization of an interim array with undefineds, as the callback function (2nd arg of Array.from) can directly run as the initial array is being written. Faster, and lower pressure on GC if browsers optimize this as the spec expressly hints at it
|
|
| S Jun 26, 2020 at 2:41 | history | suggested | Community Bot | CC BY-SA 4.0 |
not much longer
|
| Jun 25, 2020 at 21:41 | review | Suggested edits | |||
| S Jun 26, 2020 at 2:41 | |||||
| Jan 16, 2019 at 8:17 | comment | added | Stu Cox |
@icc97 Yes, linters may complain, although in JavaScript omitting a function argument defined to be the same as passing undefined, so fill() (with no argument) isn’t wrong per se. The fill value isn’t used in that solution, so if you like you could use fill(0) to save a few characters.
|
|
| Jul 17, 2018 at 14:27 | comment | added | rosenfeld | Your longer version is actually shorter if you remove the unneeded "new" before Array. | |
| Jan 9, 2018 at 2:50 | history | edited | dman | CC BY-SA 3.0 |
Wanted to give a example that is shorter with less operations
|
| Dec 13, 2017 at 16:02 | comment | added | mikebridge |
Some android devices (mainly Samsung) seem to be missing Array.from: Uncaught TypeError: Object function Array() { [native code] } has no method 'from'.
|
|
| May 17, 2017 at 5:17 | history | edited | Mike Chamberlain | CC BY-SA 3.0 |
added 291 characters in body
|
| Nov 16, 2016 at 11:25 | comment | added | Neurotransmitter |
Nifty if you'd like to create array of empty subarrays too (var matrix = Array.from(new Array(3), () => []);).
|
|
| May 15, 2016 at 7:11 | comment | added | Aditya Singh |
@Delapouite @jib And this as well: Array.from({length: end - start}, (v, k) => k + start)
|
|
| Feb 20, 2016 at 8:34 | comment | added | Stu Cox |
Slightly more succinct than the Array.from() method, and faster than both: Array(20).fill().map((_, i) => i)
|
|
| Apr 10, 2015 at 10:47 | history | answered | Kristjan Liiva | CC BY-SA 3.0 |