Category Archives: Programming

Note from JavaScript / The Definitive Guide 5th Ed.

Literals:

A literal is a data value that appears directly in a program. The following are all literals:

null // Absence of an object

In ECMAScript v3, expressions that serve as array and object literals are also supported. For example:

{ x:1, y:2 } // An object initializer
[1,2,3,4,5] // An array initializer

3. Datatypes and Values:

JavaScript defines two trivial datatypes, null and undefined, each of which defines only a single value.

3.1 Work with numbers

All numbers in JS are represented as floating point value. JavaScript represents numbers using the 64-bit floating-point format.

In addition to these basic arithmetic operations, JavaScript supports more complex mathematical operations through a large number of mathematical functions that are a core part of the language. For convenience, these functions are all stored as properties of a single Math object, so you always use the literal name Math to access them. For example, here’s how to compute the sine of the numeric value x:

sine_of_x = Math.sin(x);

3.2 String Literals

A string comprises a sequence of zero or more Unicode characters enclosed within single or double quotes (‘ or “). Double-quote characters may be contained within strings delimited by single-quote characters, and single-quote characters may be contained within strings delimited by double quotes.

‘name=”myform”‘

“Wouldn’t you prefer O’Reilly’s book?”

Numbers are automatically converted to strings when needed. If a number is used in a string concatenation expression, for example, the number is converted to a string first:

var n = 100;
var s = n + ” bottles of beer on the wall.”;

To Fix

var n = 123456.789;
n.toFixed(0);         // “123457″
n.toFixed(2);         // “123456.79″
n.toExponential(1);   // “1.2e+5″
n.toExponential(3);   // “1.235e+5″
n.toPrecision(4);     // “1.235e+5″
n.toPrecision(7);     // “123456.8″

String to number:

When a string is used in a numeric context, it is automatically converted to a number. This means, for example, that the following code actually works:

var product = “21″ * “2″; // product is the number 42.

You can take advantage of this fact to convert a string to a number by simply subtracting zero from it:

var number = string_value – 0;

ParxeXXX

parseInt(“3 blind mice”);    // Returns 3
parseFloat(“3.14 meters”);   // Returns 3.14
parseInt(“12.34″);           // Returns 12
parseInt(“0xFF”);            // Returns 255

3.4 Function

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 variables, arrays, and objects, and it means that functions can be passed as arguments to other functions. (Detail in Chapter 8 )

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 convenient and useful.

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;”);

3.6 Array

Undefined elements can be included in an array literal by simply omitting a value between commas. For example, the following array contains five elements, including three undefined elements:

var sparseArray = [1,,,,5];

3.8 undefined

Another special value used occasionally by JavaScript is the undefined value. undefined is returned when you use either a variable that has been declared but never had a value assigned to it or an object property that does not exist. Note that this special undefined value is not the same as null.

Although null and the undefined value are distinct, the == equality operator considers them equal to one another. Consider the following:

my.prop == null (not equal if “===”)