Copyright ©

Mongoose OS Forum


how to convert a string into an integer?


i have the following code: (result[1] is a string)

     GPIO.write(led, 1);
     print("Message True: ", msg, result[0], result[1]);
     Timer.set(Number(result[1]), 0, function(){
     GPIO.write(led, 0);
    }, null);

however the timer is failing because there is no integer specified....

[Jun 20 21:38:38.828] MJS callback error: [num] is not defined

+result[1] is failing
number(result[1]) is failing
parseInt(result[1],10) is failing....

any idea how to solve this?


  • SergeySergey Dublin, Ireland
    1. the immediate error is [Jun 20 21:38:38.828] MJS callback error: [num] is not defined
    2. If you exchange data, use JSON.
  • tripflextripflex Orlando, FL

    To convert a string to an integer in mJS use JSON.parse in place of parseInt

    let someVariable = "30";
    let someInt = JSON.parse( someVariable );

    Even if you're using JSON and you have already ran JSON.parse on the entire JSON, you still need to run JSON.parse on the individual key in the JSON to convert to integer (this is what got me)

Sign In or Register to comment.