# JavaScript

## Operators

Operators are the characters we use to instruct the language to do invoke string or to numbers.

### Assignment operators

An assignment operator assigns a value to its left operand based on the value of its right operand.

Name Operators Example
Assignment x = f() let x = 5
Addition assignment x += f() x = x += 6
Subtraction assignment x -= f() x = x -= 9
Multiplication assignment x *= f() x = x *= 4
Division assignment x /= f() x = x /= 2
Remainder assignment x %= f() x = x %= 3

#### Example

`let x = 3; //Assignment (x += 4); //Addition assignment console.log(x);`
`let x = 3; // Assignmentx -= 9; //Subtraction assignment console.log(x);`
`let x = 3; // Assignmentx *= 2; //Multiplication assignment console.log(x);`
`let x = 20; // Assignmentx /= 2; //Division assignment console.log(x);`
`let x = 20; // Assignmentx%= 3; //Remainder assignment console.log(x);`

### Arithmetic operators

An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value

Operators Description Example
Remainder (%) Returns the integer remainder of dividing the two operands. 12 % 5 returns 2.
Increment (++) Adds one to its operand (does the same as x = x+1) x = 3;
x++;
x=4;
Decrement (--) Subtracts one from its operand. (does the same as x = x-1) x = 3;
x--;
x=2;

#### Example

`let x = 15;let y = x % 2 //Remainder console.log(y);`
`let x = 3; x++; //Increment console.log(x);`
`let x = 19; x-- //Decrement console.log(x);`

### Logical operators

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value.

Operators Description Example
Logical AND (&&) expr1 && expr2 (true && true)
Logical OR (||) expr1 || expr2 (false || true)
Logical NOT (!) !expr (!true)

#### Example

`var a1 =  true && true; //t && t returns true`
`var a2 =  true && false; // t && f returns false`
`var o1 =  true || true; // t || t returns true`
`var o2 = false || true; // f || t returns true`
`let x = 2;let y = 3; if(x == 2 && y == 1 ){ console.log(x);};`
`let x = 2;let y = 3; if(x == 2 || y == 1 ){ console.log(x);};`

### Truth table

When we talk about logical operators is it important to look at truth tables.
The truth table tells us what the truthness of an expression is based on the truthiness of its parts.

#### Negation (!truth table)

A !A
T F
F T

This one is very straightforward.
The logical not (!) operator takes truth to falsity

#### Conjunction (The truth table &&)

A B A && B
T T T
T F F
F T F
F F F

This one shows the expression `A && B` is true, if only both A and B are true.

#### Disjunction (The truth table ||)

A B A || B
T T T
T F T
F T T
F F F

This one shows the expression `A || B` is true, if either A or B are true.

### Operator precedence

Name Operators Example
Less Than < `5 < 9 // true`
Less Than or Equal <= `5 <= 9 // true`
Greater Than > `8 > 4 // true`
Greather Than or Equal >= `9 >= 1 // true`
Equal == `9 == 1 // false`
Identical === `10 === 10 // true`
Not ! `!10 // false`
Not Equal != `9 != 10 // true`
Not identical !== `10 !== 10 //false`

### String operators

In addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings.

`console.log('my ' + 'string'); //my string`
`var mystring = 'alpha'; mystring += 'bet'; //alphabet`
`let a = 2; // Assignmentvar b = 40; // Assignmentconsole.log("the sum is " + (a+b)); //the sum is 42`
`var a = "text";var b = 40;console.log("the sum is " + (a+b)); //the sum is text40`
`var a = "2"var b = 40;console.log("the sum is " + (a+b)); //the sum is 240`

## Example

See the Pen quiz by Dominoeffekten (@dominoeffekten) on CodePen.