{"id":23164,"date":"2020-09-25T10:07:11","date_gmt":"2020-09-25T17:07:11","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=23164"},"modified":"2020-12-29T13:42:21","modified_gmt":"2020-12-29T21:42:21","slug":"javascript-range","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/javascript-range\/","title":{"rendered":"Creating Ranges with JavaScript"},"content":{"rendered":"\n<p>A lot of time when writing different functions, we might want to create ranges in order to work with some data. A range represents data in an array or object with a beginning and end value.&nbsp;<\/p>\n\n\n\n<p>A lot of languages have built in methods to create ranges, for example <code>to_a<\/code> in Ruby: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>('a'..'e').to_a =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;]<\/pre><\/div>\n\n\n\n<p>JavaScript doesn&#8217;t have a specific built-in method for this, but there are many ways we can build ranges. We\u2019ll show you three.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>_.range()<\/strong><strong> &#8211; Lodash<\/strong><\/h2>\n\n\n\n<p>If we want to create a range of numbers and we want them as fast as possible we can use the Lodash <code>_.range()<\/code> util method.<\/p>\n\n\n\n<p>This method returns an array and takes three arguments: The first for the <strong>start<\/strong> of the range, second for <strong>end<\/strong> of range (up to) and third for <strong>step<\/strong>, or value incremented\/decremented by.&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>_.range(-2);\n\/\/ =&gt; [0, -1]\n \n_.range(1, 3);\n\/\/ =&gt; [1, 2]\n \n_.range(1, 10, 4);\n\/\/ =&gt; [1, 5, 9]<\/pre><\/div>\n\n\n\n<p>Note how if only one argument is provided it will create a range up to the number specified. Also, remember Lodash is a JavaScript library that we need to install separately!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating our own <\/strong><strong>range()<\/strong><strong> function.<\/strong><\/h2>\n\n\n\n<p>If we want to save some overhead, we can create our own number range function. It turns out that making our own range is pretty easy.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function range(start, end, step = 1){\n  if(start === end) return [start];\n  const range = [];\n  for(let i = start; i &lt;= end; i += step){\n     range.push(i);\n  }\n  \n  return range;\n}\n\nrange(1, 10, 4)\n\/\/  =&gt; [1, 5, 9]\nrange(1, 10)\n\/\/ =&gt; [ 1, 2, 3, 4,  5, 6, 7, 8, 9, 10]<\/pre><\/div>\n\n\n\n<p>This is easy to write and we can avoid importing libraries such as Lodash for just one function!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a Non-number Range Function<\/strong><\/h2>\n\n\n\n<p>Now you might be asking, what about non-number ranges, like characters? There are some options for us to implement that with JavaScript.&nbsp;<\/p>\n\n\n\n<p>Here we will use the <code>charCodeAt()<\/code> method to get a number that we will then increase and then push to an array using the <code>String.fromCharCode()<\/code>, which returns a character from a character code.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function charRange(start, stop) {\n  const range = [];\n  const end = stop.charCodeAt(0)\n  for (let i = start.charCodeAt(0); i &lt;= end; i++){\n    range.push(String.fromCharCode(i));\n  }\n  return range;\n};\n\ncharRange('A','F')\n\/\/ =&gt; [ 'A', 'B', 'C', 'D', 'E', 'F' ]<\/pre><\/div>\n\n\n\n<p>That was easier than you thought right?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Now you know how easy it can be to create ranges yourself with JavaScript. Please make sure you save these handy functions, we are sure they&#8217;ll be of use at some point!<\/p>\n","protected":false},"excerpt":{"rendered":"A lot of time when writing different functions, we might want to create ranges in order to work with some data. A range represents data in an array or object with a beginning and end value.&nbsp; A lot of languages have built in methods to create ranges, for example to_a in Ruby: ('a'..'e').to_a =&gt; [&quot;a&quot;,&hellip;","protected":false},"author":86,"featured_media":2039,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-23164","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Creating Ranges with JavaScript | Career Karma<\/title>\n<meta name=\"description\" content=\"Have you ever asked yourself how to make a range with JavaScript? We&#039;ll show you different simple options to do so. Learn JavaScript with CareerKarma.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/javascript-range\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Ranges with JavaScript\" \/>\n<meta property=\"og:description\" content=\"Have you ever asked yourself how to make a range with JavaScript? We&#039;ll show you different simple options to do so. Learn JavaScript with CareerKarma.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/javascript-range\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-25T17:07:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T21:42:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/05\/developers-discussing-javascript-code.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Felipe Boh\u00f3rquez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Felipe Boh\u00f3rquez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/\"},\"author\":{\"name\":\"Felipe Boh\u00f3rquez\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e2cbf72dcbfaf9e81a8b6a38c1bd4220\"},\"headline\":\"Creating Ranges with JavaScript\",\"datePublished\":\"2020-09-25T17:07:11+00:00\",\"dateModified\":\"2020-12-29T21:42:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/\"},\"wordCount\":321,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/developers-discussing-javascript-code.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/\",\"name\":\"Creating Ranges with JavaScript | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/developers-discussing-javascript-code.jpg\",\"datePublished\":\"2020-09-25T17:07:11+00:00\",\"dateModified\":\"2020-12-29T21:42:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e2cbf72dcbfaf9e81a8b6a38c1bd4220\"},\"description\":\"Have you ever asked yourself how to make a range with JavaScript? We'll show you different simple options to do so. Learn JavaScript with CareerKarma.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/developers-discussing-javascript-code.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/05\\\/developers-discussing-javascript-code.jpg\",\"width\":1200,\"height\":800,\"caption\":\"Two men in front of computers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript-range\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Creating Ranges with JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e2cbf72dcbfaf9e81a8b6a38c1bd4220\",\"name\":\"Felipe Boh\u00f3rquez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png\",\"caption\":\"Felipe Boh\u00f3rquez\"},\"description\":\"Felipe Boh\u00f3rquez is a Software Engineer and technical writer at Career Karma. He covers all things frontend and backend development.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/felipe-bohorquez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Creating Ranges with JavaScript | Career Karma","description":"Have you ever asked yourself how to make a range with JavaScript? We'll show you different simple options to do so. Learn JavaScript with CareerKarma.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/javascript-range\/","og_locale":"en_US","og_type":"article","og_title":"Creating Ranges with JavaScript","og_description":"Have you ever asked yourself how to make a range with JavaScript? We'll show you different simple options to do so. Learn JavaScript with CareerKarma.","og_url":"https:\/\/careerkarma.com\/blog\/javascript-range\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-09-25T17:07:11+00:00","article_modified_time":"2020-12-29T21:42:21+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/05\/developers-discussing-javascript-code.jpg","type":"image\/jpeg"}],"author":"Felipe Boh\u00f3rquez","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Felipe Boh\u00f3rquez","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/"},"author":{"name":"Felipe Boh\u00f3rquez","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e2cbf72dcbfaf9e81a8b6a38c1bd4220"},"headline":"Creating Ranges with JavaScript","datePublished":"2020-09-25T17:07:11+00:00","dateModified":"2020-12-29T21:42:21+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/"},"wordCount":321,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/05\/developers-discussing-javascript-code.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/javascript-range\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/","url":"https:\/\/careerkarma.com\/blog\/javascript-range\/","name":"Creating Ranges with JavaScript | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/05\/developers-discussing-javascript-code.jpg","datePublished":"2020-09-25T17:07:11+00:00","dateModified":"2020-12-29T21:42:21+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e2cbf72dcbfaf9e81a8b6a38c1bd4220"},"description":"Have you ever asked yourself how to make a range with JavaScript? We'll show you different simple options to do so. Learn JavaScript with CareerKarma.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/javascript-range\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/05\/developers-discussing-javascript-code.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/05\/developers-discussing-javascript-code.jpg","width":1200,"height":800,"caption":"Two men in front of computers"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/javascript-range\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"Creating Ranges with JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e2cbf72dcbfaf9e81a8b6a38c1bd4220","name":"Felipe Boh\u00f3rquez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png","caption":"Felipe Boh\u00f3rquez"},"description":"Felipe Boh\u00f3rquez is a Software Engineer and technical writer at Career Karma. He covers all things frontend and backend development.","url":"https:\/\/careerkarma.com\/blog\/author\/felipe-bohorquez\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/23164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=23164"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/23164\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/2039"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=23164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=23164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=23164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}