Java Reference
In-Depth Information
You can call all methods of the Number object on all primitive numbers. However,
you have to be careful when calling methods on primitive number values. A number can
have decimal as well. The parser gets confused in the following statement:
var x = 1969.toString(16); // A SyntaxError
The parser does not know if the decimal after 1969 is part of the number or the dot to
call the
toString()
method. You have two ways to fix it:
•
You need to use two decimals in the number. The last decimal
will be considered as the dot to call the following method such as
1969..toString(16)
.
(1969).
•
Enclose the number inside a pair of parenthesis such as
toString(16)
.
The following code shows how to use methods of the Number object with primitive
numbers:
var n1 = 1969..toString(16);
print("1969..toString(16) is " + n1);
var n2 = (1969).toString(16);
print("(1969).toString(16) is " + n2);
var n3 = (1969.79).toFixed(1);
print("(1969.79).toFixed(1) is " + n3);
1969..toString(16) is 7b1
(1969).toString(16) is 7b1
(1969.79).toFixed(1) is 1969.8
The Boolean Object
The Boolean object is a function. It can be called as a function or a constructor. It takes
an optional argument. If it is called as a function, it converts the specified argument to
a Boolean primitive value (
true
or
false
). If the argument is not specified, the function
returns
false
. As a constructor, it wraps the specified argument into a Boolean object.
Notice that if the argument is not of the Boolean type, first the argument is converted to a
Boolean primitive value. For example, an argument of 100 will be converted to a Boolean
true
and stored as
true
, not as 100. Its
toString()
method returns a string “true” or
“false”, depending on the value wrapped in the Boolean object. The
valueOf()
method
Search WWH ::
Custom Search