## Javascript Course

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.

### Arithmetic operators

We can say that arithmetic operators are the main operators for numbers, they make arithmetic operations:
OperatorExample
```let a = 8;
let b = 3;
document.write(a + b); // 11
```
Subtraction: -
```let a = 8;
let b = 3;
document.write(a - b); // 5
```
Multiplication: *
```let a = 8;
let b = 3;
document.write(a * b); // 24
```
Division: /
```let a = 9;
let b = 3;
document.write(a / b); // 3
```
In addition to these four operators, there are three other arithmetic operators used in programming:
OperatorDescription /Example
Modulo: % - It determines the rest of the division of two numbers.
```let a = 8;
let b = 3;
document.write(a % b); // 2
```
Increment : ++ - Increases the value by one unit.
```let a = 9;
a++;
document.write(a); // 10
```
Decrement: -- - Substract the value by one unit.
```let a = 9;
a--;
document.write(a); // 8
```

### 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 '='.
OperatorExample
=
```let a = 8;
document.write(a); // 8
```
+=
```let a = 8;
a += 3; //Same with: a = a+3
document.write(a); // 11
```
-=
```let a = 8;
a -= 3; //Same with: a = a-3
document.write(a); // 5
```
*=
```let a = 8;
a *= 3; //Same with: a = a*3
document.write(a); // 24
```
/=
```let a = 84;
a /= 4; //Same with: a = a/4
document.write(a); // 21
```
%=
```let a = 8;
a %= 3; //Same with: a = a%3
document.write(a); // 2
```

### 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 '=='. It's different from simple '=' (this it assigns a value).
The '==' operator compares two values to determine whether they are the same, equal in value.

- List with comparation operators:
OperatorDescription /Example
== - Identical as value.
```let a = '4'; //string
let b = 4; //number

document.write( (a == b) ); //true
```
=== - Identical, as value and data type.
```let a = '4'; //string
let b = 4; //number

document.write( (a === b) ); //false
```
!= - Different as value.
```let a = 9;
let b = 4;

document.write( (a != b) ); //true
```
!== - Different as value or data type.
```let a = 4;
let b = 4;

document.write( (a !== b) ); //false
```
> - Bigger.
```let a = 9;
let b = 4;

document.write( (a > b) ); //true
```
< - Smaller.
```let a = 9;
let b = 4;

document.write( (a < b) ); //false
```
>= - Bigger or identical.
```let a = 4;
let b = 4;

document.write( (a >= b) ); //true
```
<= - Smaller or identical.
```let a = 5;
let b = 4;

document.write( (a <= b) ); //false
```

### Logical operators

The logical operators compare two expressions and return TRUE or FALSE.
OperatorDescription /Example
&& - (AND), compares two expressions and return TRUE if both are true, otherwise it returns FALSE.
```var x = 9;
var y = 4;

document.write( (x>7 && y<8) ); //true
```
|| - (OR), compares two expressions and return TRUE if at least one of them is true, otherwise it returns FALSE.
```var x = 9;
var y = 4;

document.write( (x>12 || y<8) ); //true
```
! - not, unary operator, uses a single expression and returns TRUE if the expression is false, if the expression is true, returns FALSE.
```var x = 9;
var y = 9;

document.write( !(x == y) ); //false
```

### Concatenation operator for strings

The concatenation operator (+) can also be used to join (add) string variables or text values together.
- Example:
```let str1 ='WebSite: ';
let str2 ='CoursesWeb.net';

document.write(str1 + str2); //WebSite: CoursesWeb.net
```
If the concatenation operator is used with a number and a string, the result is a string with the two merged values.
- Example:
```let x = 8; //number
let y ='9' //string

document.write(x + y); //89
```

### The conditional operator (Ternary)

The conditional operator (called Ternary) assigns a value to a variable based on a condition.
Syntax:
variable = (condition) ? val1 :val2
- First, it evaluates the condition, if it returns True, the variable takes the 'val1' value, otherwise it takes the value 'val2'.
Here is an example:
```let age = 17;
let copt = (age < 18) ? 'Young' :'Mature';

document.write(copt); // Young
```

### 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
() grouping
!   ++   -- negation, increment, decrement
*   /   % multiplication, division
<   <=   >   >= comparison
==   != equality
&& logical AND
|| logical OR
? : conditional /Ternary
=   +=   -=   *=   /=   %= assigning

#### Daily Test with Code Example

##### PHP-MySQL
Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
```<nav><ul>
<li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
<li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>```
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
```.some_class {
width: 30%;
float: left;
}```
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
```var num = 12.34567;
num = Math.floor(num);
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
```\$str = "string with utf-8 chars åèö";
\$nrchr = mb_strlen(\$str);
echo \$nrchr;        // 30```
JavaScript Operators

#### Last accessed pages

1. PHP Unzipper - Extract Zip, Rar Archives (12459)
2. Chaining multiple jQuery effects (4125)
3. Recursive function to create Multi-Level Menu in JavaScript (4710)
4. Node.js Move and Copy file (22608)
5. Refresh page if window width changes from a device size to other (565)

#### Popular pages this month

1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (520)
2. PHP Unzipper - Extract Zip, Rar Archives (423)
3. SHA256 Encrypt hash in JavaScript (402)