How/when are list items converted to float or string in js?

Martin Beck's icon

Hello, can someone explain to me how to find out if a function/property returns a number or string? I am not very experienced with weak typing programming languages. So basically I don't understand when a list is converted to number or string.
I would like to numerically add two numbers. One is coming as argument from a list fed into the [js] object. The other is coming from a matrix.getcell().

If I do notparseFloatthegetcell-result (e.g. m and n) it seems to get handled as strings and the + operator just concatenates the strings.

function list()
{
    a = arguments[0];
    b = arguments[1];    
    m = matrix.getcell(0);
    n = matrix.getcell(1);
    p = parseFloat(m);
    r = a + b;
    s = m + n;
    t = a + m;
    u = a + p;
    
    //....post results
}

typeConfusion.js
js 0.33 KB

typeConfusion.maxpat
Max Patch

Floating Point's icon

yes, for numerical values from a jitter matrix you have to pareseInt() or parseFloat(); like you I only discovered this recently while debugging-- it does not seem to be documented or explained. FYI if you are parsing ints from a matrix you can also use Math.floor() which is apparently faster than parseInt()

kLSDiz's icon

Since a matrix cell can contain more than one number (multiple planes), you get an Array object from getcell method.

Floating Point's icon

ah ok-- not immediately obvious with single-plane matrices

Martin Beck's icon

Ahh, thanks a lot. Accessing the array like this
matrix.getcell(0)[0]
returns a number without the need of parseFloat() or something like this.