Java Reference
In-Depth Information
undefined is falsy
null is falsy
100 is truthy
0 is falsy
The empty string is falsy
'Hello' is truthy
To Number Conversion
Values of all types in Nashorn can be implicitly or explicitly converted to the Number type.
Table 4-7 lists the types of values and their corresponding converted numeric values.
Table 4-7. The List of Value Types and Their Corresponding Converted Numeric Values
Value Type
Converted Number Value
Undefined
NaN
Null
+0
Boolean
The Boolean value true is converted to 1 and false to 0
Number
Identity conversion
String
The empty string and a string containing only whitespaces are
converted to zero. A string whose contents after trimming the leading
and trailing whitespaces can be interpreted as a number is converted
to the corresponding numeric value. If the numeric content of
the string is too large or too small, it is converted to +Infinity or
-Infinity , respectively. All other strings are converted to NaN
Object
If the contents of the object can be interpreted as a number, the
object is converted to the corresponding numeric value; otherwise,
the object is converted to NaN
You can use the Number() global function to explicitly convert a value to the
Number type. The function takes the value to be converted as an argument and returns
a number. The following snippet of code contains some examples of implicit and explicit
conversions to the Number type:
var result;
result = Number(undefined);
print("undefined is converted to", result);
// Any number + NaN is NaN
result = 10 + undefined;
print("10 + undefined is", result);
 
 
Search WWH ::




Custom Search