Showing posts with label JAVA Script. Show all posts
Showing posts with label JAVA Script. Show all posts

Saturday, February 26, 2011

LITERALS


Types of literals:
• Array Literals
• Boolean Literals
• Floating-Point Literals
• Integers
• Object Literals
• String Literals


ARRAY LITERALS:


An array literal is a list of zero or more expressions, each of which represents
an array element, enclosed in square brackets ([]). When you create an array
using an array literal, it is initialized with the specified values as its elements,
and its length is set to the number of arguments specified.
The following example creates the coffees array with three elements and a
length of three:
coffees = ["French Roast", "Columbian", "Kona"]


BOOLEAN LITERALS:


The Boolean type has two literal values: true and false.
Do not confuse the primitive Boolean values true and false with the true and
false values of the Boolean object. The Boolean object is a wrapper around the
primitive Boolean data type. See “Boolean Object” on page 111 for more
information.


FLOATING POINT LITERALS:


A floating-point literal can have the following parts:
• A decimal integer
• A decimal point (“.”)
• A fraction (another decimal number)
• An exponent
The exponent part is an “e” or “E” followed by an integer, which can be signed
(preceded by “+” or “-”). A floating-point literal must have at least one digit and
either a decimal point or “e” (or “E”).
Some examples of floating-point literals are 3.1415, -3.1E12, .1e12, and 2E-12


OBJECT LITERALS:


An object literal is a list of zero or more pairs of property names and associated
values of an object, enclosed in curly braces ({}). You should not use an object
literal at the beginning of a statement. This will lead to an error.
The following is an example of an object literal. The first element of the car
object defines a property, myCar; the second element, the getCar property,
invokes a function (Cars("honda")); the third element, the special
property, uses an existing variable (Sales).
var Sales = "Toyota";
function CarTypes(name) {
if(name == "Honda")
return name;
else
return "Sorry, we don’t sell " + name + ".";
}
car = {myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales}
document.write(car.myCar); // Saturn
document.write(car.getCar); // Honda
document.write(car.special); // Toyota
Additionally, you can use an index for the object, the index property (for
example, 7), or nest an object inside another. The following example uses these
options. These features, however, may not be supported by other ECMAcompliant
browsers.
car = {manyCars: {a: "Saab", b: "Jeep"}, 7: "Mazda"}
document.write(car.manyCars.b); // Jeep
document.write(car[7]); // Mazda


STRING LITERALS:


A string literal is zero or more characters enclosed in double (") or single (')
quotation marks. A string must be delimited by quotation marks of the same
type; that is, either both single quotation marks or both double quotation
marks. The following are examples of string literals:
• "blah"
• 'blah'
• "1234"
• "one line \n another line"
You can call any of the methods of the String object on a string literal value—
JavaScript automatically converts the string literal to a temporary String object,
calls the method, then discards the temporary String object. You can also use
the String.length property with a string literal.
You should use string literals unless you specifically need to use a String object.
See “String Object” on page 118 for details on String objects.





VARIABLES-DECLARING AND USING


VARIABLES:
You use variables as symbolic names for values in your application. You give
variables names by which you refer to them and which must conform to certain
rules.
A JavaScript identifier, or name, must start with a letter or underscore (“_”);
subsequent characters can also be digits (0-9). Because JavaScript is case
sensitive, letters include the characters “A” through “Z” (uppercase) and the
characters “a” through “z” (lowercase).
Some examples of legal names are Number_hits, temp99, and _name.

DECLARING VARIABLES:


You can declare a variable in two ways:
• By simply assigning it a value. For example, x = 42
• With the keyword var. For example, var x = 42
Evaluating Variables
A variable or array element that has not been assigned a value has the value
undefined. The result of evaluating an unassigned variable depends on how
it was declared:
• If the unassigned variable was declared without var, the evaluation results
in a runtime error.
• If the unassigned variable was declared with var, the evaluation results in
the undefined value, or NaN in numeric contexts.


The following code demonstrates evaluating unassigned variables.
function f1() {
return y - 2;
}
f1() //Causes runtime error
function f2() {
return var y - 2;
}
f2() //returns NaN
You can use undefined to determine whether a variable has a value. In the
following code, the variable input is not assigned a value, and the if
statement evaluates to true.
var input;
if(input === undefined){
doThis();
} else {
doThat();
}
The undefined value behaves as false when used as a Boolean value. For
example, the following code executes the function myFunction because the
array element is not defined:
myArray=new Array()
if (!myArray[0])
myFunction()
When you evaluate a null variable, the null value behaves as 0 in numeric
contexts and as false in Boolean contexts. For example:
var n = null
n * 32 //returns 0

VARIABLE SCOPE:

When you set a variable identifier by assignment outside of a function, it is
called a global variable, because it is available everywhere in the current
document. When you declare a variable within a function, it is called a local
variable, because it is available only within the function.
Using var to declare a global variable is optional. However, you must use var
to declare a variable inside a function.

You can access global variables declared in one window or frame from another
window or frame by specifying the window or frame name. For example, if a
variable called phoneNumber is declared in a FRAMESET document, you can
refer to this variable from a child frame as parent.phoneNumber.


JAVA SCRIPT- LEARN TO BUILD WEBSITES


  Here we are learning about
• Values, Variables, and Literals
• Expressions and Operators
• Regular Expressions
• Statements
• Functions
• Working with Objects
• Details of the Object Model


first let us describe wat is Values, Variables, and Literals:


VALUES:

JavaScript recognizes the following types of values:
• Numbers, such as 42 or 3.14159.
• Logical (Boolean) values, either true or false.
• Strings, such as “Howdy!”.
• null, a special keyword denoting a null value; null is also a primitive
value. Because JavaScript is case sensitive, null is not the same as Null,
NULL, or any other variant.

• undefined, a top-level property whose value is undefined; undefined is
also a primitive value.
This relatively small set of types of values, or data types, enables you to
perform useful functions with your applications. There is no explicit distinction
between integer and real-valued numbers. Nor is there an explicit date data
type in JavaScript. However, you can use the Date object and its methods to
handle dates.
Objects and functions are the other fundamental elements in the language. You
can think of objects as named containers for values, and functions as
procedures that your application can perform.

Data Type Conversion:

JavaScript is a dynamically typed language. That means you do not have to
specify the data type of a variable when you declare it, and data types are
converted automatically as needed during script execution. So, for example,
you could define a variable as follows:
var answer = 42
And later, you could assign the same variable a string value, for example,
answer = "Thanks for all the fish..."
Because JavaScript is dynamically typed, this assignment does not cause an
error message.
In expressions involving numeric and string values with the + operator,
JavaScript converts numeric values to strings. For example, consider the
following statements:
x = "The answer is " + 42 // returns "The answer is 42"
y = 42 + " is the answer" // returns "42 is the answer"
In statements involving other operators, JavaScript does not convert numeric
values to strings. For example:
"37" - 7 // returns 30
"37" + 7 // returns 377



Tweet