Site hosted by Angelfire.com: Build your free website today!
Website Help

| HTML | CSS | JavaScript |

Soon to come:
XHTML, XML, ASP,
Home

JS Basics

JS Home
Introduction
Basics
Operators
Functions
Conditional
Looping

JS References

String Object
Array Object
Date Object
Math Object
Window Object
Frame Object
Form Object
Browser Object

JavaScript Operators

In order to input, evaluate, manipulate, or display data, you need operators. Operators are symbols and identifiers that represent the way that the data is changed or the way a combination of expressions is evaluated. JavaScript supports both binary and unary operators. Binary operators are operators two operands, x+y, while unary take only one operand, x++. Operands are the variables which the operators refer to.

Assignment operators

Combination of assignment and arithmetic Operators


    x += y is short for x = x + y
    x -= y is short for x = x - y
    x *= y is short for x = x * y
    x /= y is short for x = x / y
    x %= y is short for x = x % y
    

Combinations of Assignment and Bitwise Operators


    x <<= y is short for x = x << y
    x >>= y is short for x = x >> y
    x >>>= y is short for x = x >>> y
    x &= y is short for x = x & y
    x ^= y is short for x = x ^ y
    x |= y is short for x = x | y
    

Arithmetic Operators

Arithmetic operators


    + addition
    - subtraction
    * multiplication
    / division
    x++ short for x = x + 1
    x-- short for x = x - 1
(you can use the ++, and -- operators as either prefixes or suffixes, ++x is same as x++)

Comparison Operators

Comparison Operators are used for comparing. The answer can be either true or false.

Comparison Operators


Operator

Description

== Equal operator. Returns (evaluates) true if both operands are equal.
!= Does not equal operator. Returns true if its operands are not equal.
> Greater-than operator. Returns true if its left operand is greater in value than its right operand.
>= Greater-than-or-equal-to operator. Returns true if its left operand is greater than or equal to its rightr operand.
< Less-than operator. Returns true if its left operand is less than the value of its right operand.
<= Less-than-or-equal-to operator. Returns true if its left operand is less than or equal to its right operand.

String Operators

+ operator is used to concantate strings, eg; if i have 2 strings "atgc" and "ttcg" and I put it into the document.write() method then document.write("atgc" + "ttcg") would display on the page "atgcttcg"


Conditional Operators

JavaScript uses two conditional operators, ? and :, to form conditional expressions. it is similar to the if statement. Conditional expressions return one of two values depending on the logical value of another expression. consider the following:

    var aNumber = 98
    var resultMsg = (aNumber == 89) ? "Same number" : "Different number";
    alert(resultMsg);
    

The above would return and alert Different number The reason because aNumber contains the value 98 however the test statement makes it return true if aNumber == 89, the statement left of : is displayed if the test condition is true, while the statemetn right of : is displayed when the test condition is false.


Boolean Operators

Boolean operators (logical operators) are used in conjuction with expressions that return logical values.

Boolean Operators


Operator

Description

&&

Logical AND operator, returns true if 2 expressions return true. Eg:
(1 > 0) && (2 > 1) returns true.
(2 > 0) && (4 > 6) returns false. ...4 is not greater than 6.

||

Logical OR operator, returns true if one of two expressions evaluates to be true. Eg:
(1 > 0) || (2 > 4) returns true.
(1 > 2) || (0 > 1) returns false.

!

Logical NOT operator, it is a unary operator which returns the opposite value of a Boolean expresssion. if expression is true, it returns false, and if expressions is false, it returns true. Eg:
!(1 > 0) returns false.
!(1 > 2) returns true.

The typeof Operator

The typeof operator returns the type of data that its operand currently holds.

typeof unescape returns the string "function".
typeof 33 returns the string "number".
typeof "this String" returns the string "string".
typeof true returns the string "boolean".
etc...
fastadvert.com


fastadvert.com - make money with your website