Data Type Conversion
1. Array length
Truncating an array by setting its length property is the only way that you can actually shorten an array. If you use the delete operator to delete an array element, that element becomes undefined, but the length property does not change.
2. Array.concat( )
If any of these arguments is itself an array, it is flattened and its elements are added to the returned array. Note, however, that concat( ) does not recursively flatten arrays of arrays. Here are some examples:
var a = [1,2,3];
a.concat([4,5],[6,7]) // Returns [1,2,3,4,5,6,7]
a.concat(4, [5,[6,7]]) // Returns [1,2,3,4,5,[6,7]]
//indicate that concat( ) does not recursively flatten arrays of arrays
3. Object-to-Primitive Conversion
a. object->boolean:
whenever a non-null object is used in a boolean context, it converts to true. This is true for all objects (including all arrays and functions), even wrapper objects that represent primitive values that convert to false. For example:
a[0]=new Boolean(false) // Internal value is false, but object converts to true
a[1]=new Number(0)
a[2]=new String("")
a[3]=new Array( )
if(a[0]&&a[1]&&a[2]&&a[3]){
document.write("all are true");
}
//output: all are true.
b. object->number:
objects are converted to numbers by first calling the valueOf( ) method of the object. Most objects inherit the default valueOf( ) method of Object, which simply returns the object itself. Since the default valueOf( ) method does not return a primitive value, JavaScript next tries to convert the object to a number by calling its toString( ) method and converting the resulting string to a number.
Array->number:
Call the toString( ) method of arrays converts the array elements to strings, then returns the result of concatenating these strings, with commas in between. Therefore, an array with no elements converts to the empty string, which converts to the number zero! Also, if an array has a single element that is a number n, the array converts to a string representation of n, which is then converted back to n itself. If an array contains more than one element, or if its one element is not a number, the array converts to NaN.For example:
b=new Array();
b[0]="1";
document.write(10-b); //output:9
document.write(10+b); //output:101
b[0]="w";
document.write(10-b); //output:NaN
document.write(10+b); //output:10w
b[1]=3;
document.write(10-b); //output:NaN
document.write(10+b); //output:10w,3
Strings ->Numbers
parseInt( ) and parseFloat( ) functions convert and return any number at the beginning of a string, ignoring any trailing non-numbers. (P.S. This point is different from Java which will throw java.lang.NumberFormatException).
parseInt( ) can even take a second argument specifying the radix (base) of the number to be parsed. Legal values are between 2 and 36. For example:
var a="12,3";
document.write(a-1); // output: NaN
document.write(parseInt(a)); // output:12
document.write(parseInt(a-1)); // output: NaN
document.write(parseInt("11", 2)); // output: 3 (1*2 + 1)