Table 2.1 summarizes all of the JavaScript operators.
Table 2.1 JavaScript operators.
Implemented in | Navigator 2.0 |
The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x. The other assignment operators are shorthand for standard operations, as shown in Table 2.2.
Table 2.2 Assignment operators
Implemented in | Navigator 2.0 |
They are described in Table 2.3. In the examples in this table, assume var1
has been assigned the value 3 and var2
had been assigned the value 4.
Table 2.3 Comparison operators
Implemented in | Navigator 2.0 |
var1 % var2The modulus operator returns the first operand modulo the second operand, that is,
var1
modulo var2
, in the preceding statement, where var1
and var2
are variables. The modulo function is the integer remainder of dividing var1
by var2
. For example, 12 % 5 returns 2.y = -x
negates the value of x
and assigns that to y
; that is, if x
were 3, y
would get the value -3 and x
would retain the value 3.Table 2.4 summarizes JavaScript's bitwise operators
Implemented in | Navigator 2.0 |
Implemented in | Navigator 2.0 |
Shift operators convert their operands to thirty-two-bit integers and return a result of the same type as the left operator.
For example, 9<<2
yields thirty-six, because 1001 shifted two bits to the left becomes 100100, which is thirty-six.
For example, 9>>2 yields two, because 1001 shifted two bits to the right becomes 10, which is two. Likewise, -9>>2 yields -3, because the sign is preserved.
For example, 19>>>2 yields four, because 10011 shifted two bits to the right becomes 100, which is four. For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result.
Implemented in | Navigator 2.0 |
They are described in Table 2.5.
<script language="JavaScript1.2">"
v1 = "Cat";
v2 = "Dog";
v3 = false;
document.writeln("t && t returns " + (v1 && v2));
document.writeln("f && t returns " + (v3 && v1));
document.writeln("t && f returns " + (v1 && v3));
document.writeln("f && f returns " + (v3 && (3 == 4)));
document.writeln("t || t returns " + (v1 || v2));
document.writeln("f || t returns " + (v3 || v1));
document.writeln("t || f returns " + (v1 || v3));
document.writeln("f || f returns " + (v3 || (3 == 4)));
document.writeln("!t returns " + (!v1));
document.writeln("!f returns " + (!v3));
</script>This script displays the following:
t && t returns Dog
f && t returns false
t && f returns false
f && f returns false
t || t returns Cat
f || t returns Cat
t || f returns Cat
f || f returns false
!t returns false
!f returns true
false
&& anything is short-circuit evaluated to false.true
|| anything is short-circuit evaluated to true."my " + "string"
returns the string "my string"
.
Implemented in | Navigator 2.0 |
The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring
has the value "alpha," then the expression mystring += "bet"
evaluates to "alphabet" and assigns this value to mystring
.
if
statement.
Implemented in | Navigator 2.0 |
condition ? expr1 : expr2
condition |
an expression that evaluates to true or false
|
expr1, expr2 | expressions with values of any type. |
condition
is true
, the operator returns the value of expr1
; otherwise, it returns the value of expr2
. For example, to display a different message based on the value of the isMember
variable, you could use this statement:document.write ("The fee is " + (isMember ? "$2.00" : "$10.00"))
Implemented in | Navigator 2.0 |
expr1, expr2
expr1, expr2 | Any expressions |
for
loop. for (var i=0, j=10; i <= 10; i++, j--)
document.writeln("a["+i+","+j+"]= " + a[i,j])
Implemented in | Navigator 2.0 |
delete objectName.property
delete objectName[index]
delete property
objectName | The name of an object. |
property | An existing property. |
index | An integer representing the location of an element in an array |
with
statement.
If the deletion succeeds, the delete
operator sets the property or element to undefined
. delete
always returns undefined.
Implemented in | Navigator 2.0 |
objectName = new objectType (param1 [,param2] ...[,paramN])
You can always add a property to a previously defined object. For example, the statement car1.color = "black"
adds a property color
to car1
, and assigns it a value of "black"
. However, this does not affect any other objects. To add the new property to all objects of the same type, you must add the property to the definition of the car
object type.
You can add a property to a previously defined object type by using the Function.prototype
property. This defines a property that is shared by all objects created with that function, rather than by just one instance of the object type. The following code adds a color
property to all objects of type car
, and then assigns a value to the color
property of the object car1
. For more information, see prototype
Car.prototype.color=null
car1.color="black"
birthday.description="The day you were born"
car
, and you want it to have properties for make, model, and year. To do this, you would write the following function:function car(make, model, year) {Now you can create an object called
this.make = make
this.model = model
this.year = year
}
mycar
as follows:mycar = new car("Eagle", "Talon TSi", 1993)This statement creates
mycar
and assigns it the specified values for its properties. Then the value of mycar.make
is the string "Eagle"
, mycar.year
is the integer 1993
, and so on.
You can create any number of car
objects by calls to new
. For example,
kenscar = new car("Nissan", "300ZX", 1992)Example 2: object property that is itself another object. Suppose you define an object called
person
as follows:function person(name, age, sex) {And then instantiate two new
this.name = name
this.age = age
this.sex = sex
}
person
objects as follows:rand = new person("Rand McNally", 33, "M")Then you can rewrite the definition of
ken = new person("Ken Jones", 39, "M")
car
to include an owner property that takes a person
object, as follows:function car(make, model, year, owner) {To instantiate the new objects, you then use the following:
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
car1 = new car("Eagle", "Talon TSi", 1993, rand);Instead of passing a literal string or integer value when creating the new objects, the above statements pass the objects
car2 = new car("Nissan", "300ZX", 1992, ken)
rand
and ken
as the parameters for the owners. To find out the name of the owner of car2
, you can access the following property:car2.owner.name
this
refers to the calling object.
Implemented in | Navigator 2.0 |
this
[.propertyName]
validate
validates an object's value property, given the object and the high and low values:function validate(obj, lowval, hival) {You could call
if ((obj.value < lowval) || (obj.value > hival))
alert("Invalid Value!")
}
validate
in each form element's onChange
event handler, using this
to pass it the form element, as in the following example:<B>Enter a number between 18 and 99:</B>
<INPUT TYPE = "text" NAME = "age" SIZE = 3
onChange="validate(this, 18, 99)">
typeof
operator is used in either of the following ways:1. typeof operandThe
2. typeof (operand)
typeof
operator returns a string indicating the type of the unevaluated operand. operand
is the string, variable, keyword, or object for which the type is to be returned. The parentheses are optional.
Implemented in | Navigator 3.0 |
Suppose you define the following variables:
var myFun = new Function("5+2")The
var shape="round"
var size=1
var today=new Date()
typeof
operator returns the following results for these variables:typeof myFun is objectFor the keywords
typeof shape is string
typeof size is number
typeof today is object
typeof dontExist is undefined
true
and null
, the typeof
operator returns the following results:typeof true is booleanFor a number or string, the
typeof null is object
typeof
operator returns the following results:typeof 62 is numberFor property values, the
typeof 'Hello world' is string
typeof
operator returns the type of value the property contains:typeof document.lastModified is stringFor methods and functions, the
typeof window.length is number
typeof Math.LN2 is number
typeof
operator returns results as follows:typeof blur is functionFor predefined objects, the
typeof eval is function
typeof parseInt is function
typeof shape.split is function
typeof
operator returns results as follows:typeof Date is function
typeof Function is function
typeof Math is function
typeof Option is function
typeof String is function
1. javascript:void (expression)The void operator specifies an expression to be evaluated without returning a value.
2. javascript:void expression
expression
is a JavaScript expression to evaluate. The parentheses surrounding the expression are optional, but it is good style to use them.
Implemented in | Navigator 3.0 |
You can use the void
operator to specify an expression as a hypertext link. The expression is evaluated but is not loaded in place of the current document.
<A HREF="javascript:void(0)">Click here to do nothing</A>The following code creates a hypertext link that submits a form when the user clicks it.
<A HREF="javascript:void(document.form.submit())">
Click here to submit</A>
Last Updated: 10/31/97 12:29:53