3.3. Boolean ValuesThe number and string datatypes have a large or infinite number of possible values. The boolean datatype, on the other hand, has only two. These two values are represented by the literals TRue and false . A value of type boolean represents a truth value; it says whether something is true or not.
Boolean values are
a == 4 This code tests to see whether the value of the variable a is equal to the number 4 . If it is, the result of this comparison is the boolean value true . If a is not equal to 4 , the result of the comparison is false .
Boolean values are typically used in JavaScript control structures. For example, the
if/else
statement in JavaScript
if (a == 4) b = b + 1; else a = a + 1;
This code checks whether
a
equals
4
. If so, it adds
1
to
b
;
Instead of thinking of the two possible boolean values as
TRue
and
false
, it is sometimes
3.3.1. Boolean Type Conversions
Boolean values are easily
If a number is used where a boolean value is expected, the number is converted to TRue unless the number is 0 or NaN , which are converted to false . If a string is used where a boolean value is expected, it is converted to true except for the empty string, which is converted to false . null and the undefined value convert to false , and any non- null object, array, or function converts to true . If you prefer to make your type conversions explicit, you can use the Boolean( ) function: var x_as_boolean = Boolean(x); Another technique is to use the Boolean NOT operator twice: var x_as_boolean = !!x; |
3.4. Functions
A
function
is a piece of executable code that is defined by a JavaScript program or predefined by the JavaScript implementation. Although a function is defined only once, a JavaScript program can execute or invoke it any number of times. A function may be passed
arguments
, or
parameters
, specifying the value or values upon which it is to perform its computation, and it may also return a value that represents the results of that computation. JavaScript
JavaScript programs may also define their own functions with code that looks like this:
function square(x) // The function is named square. It expects one argument, x.
{ // The body of the function begins here.
return x*x; // The function squares its argument and returns that value.
} // The function ends here.
Once a function is defined, you can invoke it by following the function's
y = Math.sin(x); y = square(x); d = compute_distance(x1, y1, z1, x2, y2, z2); move( );
An important feature of JavaScript is that functions are values that can be manipulated by JavaScript code. In many languages, including Java, functions are only a syntactic feature of the language: they can be defined and invoked, but they are not datatypes. The fact that functions are true values in JavaScript gives a lot of flexibility to the language. It means that functions can be stored in
Since functions are values just like
3.4.1. Function Literals
The
function square(x) { return x*x; }
it can be defined with a function literal:
var square = function(x) { return x*x; }
Functions defined in this way are sometimes called
lambda functions
in homage to the Lisp programming language, which was one of the first to allow unnamed functions to be embedded as literal data values within a program. Although it is not immediately obvious why you might choose to use function literals in a program, I'll show you later how in advanced scripts, they can be quite
There is one other way to define a function: you can pass the argument list and the body of the function as strings to the Function( ) constructor. For example:
var square = new Function("x", "return x*x;");
Defining a function in this way is not often useful. It is usually
|