Operators take one or more variables or values (operands) and return a new value; e.g. the '+' operator can add two numbers to produce a third. You use operators in expressions to relate values, whether to perform arithmetic or compare quantities. Operators are divided into several classes depending on the relation they perform:

arithmetic or computational

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are:

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus: the remainder after division;

e.g. 10 % 3 yields 1.

++ Unary increment: this operator only takes one operand. The operand's value is increased by 1. The value returned depends on whether the ++ operator is placed before or after the operand; e.g. ++x will return the value of x following the increment whereas x++ will return the value of x prior to the increment.

-- Unary decrement: this operator only takes one operand. The operand's value is decreased by 1. The value returned depends on whether the -- operator is placed before or after the operand; e.g. --x will return the value of x following the decrement whereas x-- will return the value of x prior to the decrement.

- Unary negation: returns the negation of operand.

comparison

A comparison operator compares its operands and returns a logical value based on whether the comparison is true or not. The operands can be numerical or string values. When used on string values, the comparisons are based on the standard lexicographical (alphabetic) ordering.

== "Equal to" returns true if operands are equal.

!= "Not equal to" returns true if operands are not equal.

> "Greater than" returns true if left operand is greater than right operand.

>= "Greater than or equal to" returns true if left operand is greater than or equal to right operand.

< "Less than" returns true if left operand is less than right operand.

<= "Less than or equal to" returns true if left operand is less than or equal to right operand.

boolean

Boolean operators are typically used to combine multiple comparisons into a conditional expression. For example, you might want to test whether (total>100) AND (stateTax=true). A boolean operator takes two operands, each of which is a true or false value, and returns a true or false result.

&& "And" returns true if both operands are true.

|| "Or" returns true if either operand is true.

! "Not" returns true if the negation of the operand is true (e.g. the operand is false).

string

Strings can be compared using the comparison operators. Additionally, you can concatenate strings using the + operator.

"dog" + "bert"

yields

"dogbert"

assignment

The assignment operator (=) lets you assign a value to a variable. You can assign any value to a variable, including another variable (whose value will be assigned). Several shorthand assignment operators allow you to perform an operation and assign its result to a variable in one step.

= Assigns the value of the righthand operand to the variable on the left.

Example: total=100;

Example: total=(price+tax+shipping)

+=

(also -=, *=, /=) Adds the value of the righthand operand to the lefthand variable.

Example: total+=shipping (adds value of shipping to total and assigned result to total)

&=

(also |=) Assigns result of (lefthand operand && righthand operand) to lefthand operand.

special

Several JavaScript operators, rarely used, fall into no particular category. These operators are summarized below.

Conditional operator

(condition) ? trueVal : falseVal Assigns a specified value to a variable if a condition is true, otherwise assigns an alternate value if condition is false.

Example:

preferredPet = (cats > dogs) ? "felines" : "canines"

If (cats>dogs), preferredPet will be assigned the string value "felines," otherwise it will be assigned "canines".

typeof operand Returns the data type of operand.

Example -- test a variable to determine if it contains a number:

if (typeof total=="number") ...

regular expressions (Netscape & MSIE 4)

New to JavaScript 1.2 is support for regular expressions, which are defined patterns used to match character combinations appearing in string values. Regular expressions are very powerful, potentially allowing you to search for any conceivable character pattern. However, they can also be quite complex to construct. Because regular expressions are widely supported in all high-level development environments, it is advised that you consider learning about regular expressions as a subject unto itself.

Two detailed explanations of regular expressions can be found at

Netscape's JavaScript Guide: Chapter 9 Expressions and Operators

PERL5 Regular Expression Description

our essay writing team will work on it relent lessly to ensure that its
quality

## 0 comments:

Post a Comment