I want to test whether a JavaScript variable has a value.
var splitarr = mystring.split( ' ' ); aparam = splitarr [0] anotherparam = splitarr [1] //.... etc
However the string might not have enough entries so later I want to test it.
if ( anotherparm /* contains a value */ )
How do I do this?
In general it’s sort of a gray area… what do you mean by ‘has a value’? The values
nullandundefinedare legitimate values you can assign to a variable…The String function
split()always returns an array so use thelengthproperty of the result to figure out which indices are present. Indices out of range will have the valueundefined.But technically (outside the context of
String.split()) you could do this: