Search code examples
javascriptecmascript-6lodash

range is not a function in lodash?


I have lodash cdn included in this fiddle https://jsfiddle.net/byhh6Lyo/

console.log(_)
console.log(_.range(1, 10))
_.map([1,2,3], (o) => console.log(o)) //worked

The second line has an error, saying range is not a function, what's wrong?


Solution

  • The cdn you are using is not correct. You were using core version which does not contain _.range. core build has very limited features available. Check here for more information : https://github.com/lodash/lodash/wiki/Build-Differences

    Solution: Use the full version => https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js

    Below is working fine:

    console.log(_)
    console.log(_.range(1, 10))
    _.map([1, 2, 3], (o) => console.log(o)) //worked
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>