With operators we can manipulate, combine and modify data in a program or script. There are many kinds of operators, in this lesson it is presented the types of operators used in JavaScript.

### 1. Arithmetic operators

We can say that arithmetic operators are the main operators for numbers, they make arithmetic operations:
• = (used to asign values) - eg, (x=8)
• + (used to add values) - eg, (x=7+8)
• - (substraction) - eg, (x=7-8)
• * (multiplication) - eg, (x=7*8)
• / (division) - eg, (x=7/8)
- Besides these operators there are other special operators used in programming.
• Modulo (%) - it determines the rest of the division of two numbers
Example:
• 8%3 result 2
10%2 result 0
• Increment (++) - This operator increases the value by one unit, is often used in programming.
• For example, if we have a variable 'i', we can use the increment operator: i++ which is similar to i = i+1.
Example:
• x = 7;
x++;
the result is x = 8
• Decrement (--) - This operator substract the value by one unit.
• If we have a variable 'i', we can use the decrement operator: i-- which is similar to i = i-1.
Example:
• x = 7;
x--;
the result is x = 6
• These two operators can be also placed before the variable name (++i, --i). The result value is the same, but the order of operation is different. In this case, first modify the value and then it assign it to the variable.

### 2. Assigning operators

These operators evaluate first the operand on the right and the value is assigned to the variable of the left side of the sign "=".
 Operator Example Same as = x = y x = y += x += y x = x+y -= x -= y x = x-y *= x *= y x = x*y /= x /= y x = x/y %= x %= y x = x%y

### 3. Comparation operators

Expressions using these operators make a question about two values which they compare. The answer can be TRUE or FALSE.
A commonly used comparison operator is the identity operator, represented by two equal signs "==".

Comparison operators are presented below:
 Operator Meaning Example == Identical 3 == 8   result FALSE != Different 3 != 8   result TRUE > Bigger 3 > 8   result FALSE < Smaller 3 < 8   result TRUE >= Bigger or identical 3 >= 8   result FALSE <= Smaller or identical 3 <= 8   result TRUE

### 4. Logical operators (booleans)

The logical operators compare two expressions and return TRUE or FALSE.
• && - AND - It compare two expressions and return TRUE if both are true, otherwise it returns FALSE.
• x = 5
y = 8
x<7 && y>3
(return TRUE)
• || - OR - It compare two expressions and return TRUE if at least one of them is true, otherwise it returns FALSE.
• x = 5
y = 8
x>7 || y<3
(return FALSE)
• ! - NOT - unary operator, uses a single expression and returns TRUE if the expression is false, if the expression is true, returns FALSE.
• x = 5
y = 8
!(x==y)
(it returns TRUE becouse 'x' and 'y' are different)

### 5. Operator used on strings

The "+" operator can also be used to join (add) string variables or text values together.
• t1 = "Have a good "
t2 = "life."
t3 = t1+t2

(The 't3' variable will contain the string "Have a good life.")

### 6. The typeof operator

This operator returns the data type of its operand. It is especially useful to determine if a variable is defined with a specific data type.
Studying the table below you can understand how thi operator works:
 Operator Description typeof parseFloat returns the string 'function' typeof 33 returns the string 'number' typeof "some text" result 'string' typeof true returns the string 'boolean' typeof window returns the string 'object'

### 7. Operators used on functions

Functions will be explained in one of the following lessons, however it is useful to be familiar with two operators:

1. The first is known as calling operator and is represented by a pair of parentheses () who are always after the function name. A function is declared as follows:

```function function_name() {
// Instructions
}
```
Then, the "callig operator" is used when we call the function in the script:
• function_name()
The parentheses shows that we use a function.

The second operator for functions is the comma operator (,) which is used to separate multiple arguments that a function receives.
The arguments are always written inside parenthesis, and separated by commas.
A function with two arguments would look like:
```function function_name(arg1, arg2) {
// Corpul functiei
}
```

### 8. Operators used on objects

1. The main operator for objects is the character point (.). This operator allows us to refer to a member (variable, function or object) that belongs to the specified object.
The syntax is:
• objectName.variable_name
objectName.function_name()
objectName.childObject
This way of referring to an information, called point notation, returns the value of variable, function or object at the right.

2. The operator for array elements, a pair of brackets [], called the array index operator, allow us to refer to any member of an array.
JavaScript arrays are objects and will be detailed in subsequent lessons.
The syntax for using this operator is
• array_name[key]

### 9. Conditional operator "? :"

JavaScript contains a conditional operator ? : that assigns a value to a variable based on condition.
The syntax for using this operator:
• variable = (condition) ? val1 : val2;
- It evaluates the condition, if it's True then the variable takes the value 'VAL1', otherwise, takes the value 'VAL2'.
Here's an example:
```<script type="text/javascript">
var visitor = "man";
message = (visitor=="man") ? "Sir. " : "Dear Madam";
document.write(message)
</script>
```
If the variable "visitor" has the value 'man', the variable "message" gets the value 'Sir.', otherwise receives the value "Dear Madam". And the display instruction "document.write()" will display the value of "message".

### 10. Operators precedence

When in expressions are used many operators, the JavaScript takes into account the precedence (the importance) of each operator. As in arithmetic, in an equation with multiplication and addition (2 + 3 * 4), if there aren't parentheses, multiplication is performed first. The same thing is for the programming operators, too.
If there are several operators with the same precedence, JavaScript will evaluate them from left to right.
The following table apresents in order the precedence of the operators:
 Operator Description ()   []   . for calling, data structures !   ++   -- negation, increment *   /   % multiplication, division +   - addition, subtraction <   <=   >   >= comparison ==   != equality && logical AND || logical OR ? : conditionnal =   +=   -=   *=   /=   %= assigning , comma

#### Daily Test with Code Example

##### PHP-MySQL
Which tag is used to include external CSS file in web page?
<body> <script> <link>
`<link href="/templ/style.css" rel="stylesheet" type="text/css" />`
Which CSS property sets the text size?
font-weight text-decoration font-size
```h2 {
font-size: 1em;
}```
Indicate the JavaScript property that can add HTML code into an element.
text value innerHTML
`document.getElementById("someID").innerHTML = "HTML content";`
Click on the function that returns the number of characters of a string in PHP.
count() strlen() stristr()
```\$str = "http://CoursesWeb.net/";
\$nr_chr = strlen(\$str);
echo \$nr_chr;       // 22```
JavaScript Operators

#### Last accessed pages

1. Save image on server from external URL (2043)
2. PHP Code Snippets (4378)
3. PHP OOP - Constructor Method (3870)
4. Theme Hotel (2918)
5. Functions, Variable scope and Passing by Reference (1452)

#### Popular pages this month

1. Flash Games - Free online Games (2254)
2. Qwop (1667)
3. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1497)
4. Tunnel Rush (1348)
5. Drag Racer V3 (1092)