Skip to main content

Featured post

How to Insert line break in Text component in React Native

Hi, I was developing an application in react native.I have to handle lots of text data in this small react native application.I have used  Text component in react native many times. While working on this application,i had a requirement to display some text content with line breaks.I will explain it clearly with an example.I want to display a text as shown below  Welcome Please subscribe my channel I wrote react native code as shown below <View> <Text> Welcome Please subscribe my channel </Text> <View> When i run these lines of code,It will display text as shown below Welcome Please subscribe my channel   - instead of displaying in two lines with line break,it displays in a single line.So i have to find a solution to insert line break in Text component in React Native.After lots of research,i found a clean solution as shown below <View> <Text> Welcome{"\n"} Please subscribe my channel </Text> <View> Here ...

RangeError: Invalid array length error fixed in javascript with examples

Dear friends,today we discuss about two types of errors which related to  each others 

  1. RangeError: Invalid array length.
  2. RangeError: Array size is not a small enough positive integer.


Those errors are thrown from V8,SpiderMoneky  and JavaScriptCore javascript engines from web browsers at front end and node js like javascript run time environment from back end 



So we will understand Invalid array length and Array size is not a small enough positive integer error and different cases of getting errors  related to javascript array length  in our coding.



1.declare an array   object by Array() constructor function giving negative number as array length 



let us check this with an example 


let arr = new Array(-10)


we will get the output as shown below


let arr = new Array(-10)

          ^


RangeError: Invalid array length

    at Object.<anonymous> (C:\Users\Administrator\Desktop\JS\arraylength.js:1:11)        

    at Module._compile (node:internal/modules/cjs/loader:1226:14)

    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)

    at Module.load (node:internal/modules/cjs/loader:1089:32)

    at Module._load (node:internal/modules/cjs/loader:930:12)

    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

    at node:internal/main/run_main_module:23:47


Node.js v18.14.0






2.assign a floating point number to length property of the array 


let  me  explain it with an example. I declared an array and tried to change its length with floating point number


let arr = [];

arr.length = 1.5;


when we run the above code,we will get the result as shown below


arr.length = 1.5;

           ^


RangeError: Invalid array length

    at Object.<anonymous> (C:\Users\Administrator\Desktop\JS\arraylength.js:2:12)        

    at Module._compile (node:internal/modules/cjs/loader:1226:14)

    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)

    at Module.load (node:internal/modules/cjs/loader:1089:32)

    at Module._load (node:internal/modules/cjs/loader:930:12)

    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

    at node:internal/main/run_main_module:23:47


Node.js v18.14.0






3.Assign length of the array with big value greater than 2^32-1  


we can check it with an example


let arr1 = new Array(10000000000000000000000000000000000000000000000000000000000000000000000000000000)

let arr2= new Array(Math.pow(2, 32))



if we execute both lines we will get the same error as shown below


let arr1 = new Array(10000000000000000000000000000000000000000000000000000000000000000000000000000000)

           ^


RangeError: Invalid array length

    at Object.<anonymous> (C:\Users\Administrator\Desktop\JS\arraylength.js:1:12)

    at Module._compile (node:internal/modules/cjs/loader:1226:14)

    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)

    at Module.load (node:internal/modules/cjs/loader:1089:32)

    at Module._load (node:internal/modules/cjs/loader:930:12)

    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

    at node:internal/main/run_main_module:23:47


Node.js v18.14.0





finally we can avoid  errors related to array length like Invalid array length RangeError by not using floating point number,negative number and number greater that  2^32-1   as array length