# JavaScript Operators

In JavaScript, operators are essential tools used for performing various mathematical and logical computations on values.

**The Assignment Operator (=)**assigns values.**The Addition Operator (+)**adds values together.**The Multiplication Operator (*)**multiplies values.**The Comparison Operator (>)**compares values to determine which is greater

## JavaScript Assignment

The Assignment Operator (** =**) is used in JavaScript to assign a value to a variable.

**Assignment Examples:**

assigns the value 10 to the variable`let x = 10;`

`x`

**JavaScript Addition**

****The Addition Operator (** +**) is used to add numbers together in JavaScript.

**Example of Addition:**

- Initialize
with 5 and`x`

with 2.`y`

- The result of
is stored in`x + y`

, so`z`

equals 7.`z`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The + Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
```

## JavaScript Multiplication

The Multiplication Operator (** ***) is used to multiply numbers in JavaScript.

**Example of Multiplication:**

is assigned the value 5, and`x`

is assigned the value 2.`y`

- The product of
and`x`

is calculated using`y`

and the result, 10, is stored in`x * y`

.`z`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The * Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x * y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
```

## Types of JavaScript Operators

There are different types of JavaScript operators:

- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- String Operators
- Logical Operators
- Bitwise Operators
- Ternary Operators
- Type Operators

**JavaScript Arithmetic Operators**

****Arithmetic operators in JavaScript are used to perform common mathematical operations on numbers.

**Example of Using Arithmetic Operators:**

is set to 3.`a`

is calculated by adding 100 and 50, then multiplying the result by`x`

, which results in`a`

being 450.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Arithmetic Operations</h2>
<p>A typical arithmetic operation takes two numbers (or expressions) and produces a new number.</p>
<p id="demo"></p>
<script>
let a = 3;
let x = (100 + 50) * a;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
```

Here's a list of some common JavaScript arithmetic operators along with their descriptions:

**+**: Addition**-**: Subtraction*****: Multiplication******: Exponentiation (introduced in ES2016)**/**: Division**%**: Modulus (Division Remainder)**++**: Increment**--**: Decrement

**JavaScript Assignment Operators**

****Assignment operators in JavaScript are used to assign values to variables.

**The Addition Assignment Operator (**: This operator adds a value to a variable and then reassigns the sum back to the same variable.`+=`

)

**Example of Assignment:**

is initially set to 10.`x`

increases the value of`x += 5`

by 5, making the new value of`x`

15.`x`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The += Operator</h2>
<p id="demo"></p>
<script>
var x = 10;
x += 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
```

Here's a list of JavaScript assignment operators along with examples and their equivalent long-form expressions:

**=**:— Same as`x = y`

`x = y`

**+=**:— Same as`x += y`

`x = x + y`

**-=**:— Same as`x -= y`

`x = x - y`

*****=:— Same as`x *= y`

`x = x * y`

**/=**:— Same as`x /= y`

`x = x / y`

**%=**:— Same as`x %= y`

`x = x % y`

****=**:— Same as`x **= y`

`x = x ** y`

Here’s a list of JavaScript comparison operators along with their descriptions:

**==**: Equal to**===**: Equal value and equal type**!=**: Not equal**!==**: Not equal value or not equal type**>**: Greater than- **

## JavaScript String Comparison

All the comparison operators mentioned can be used for string comparisons in JavaScript as well:

Example: If you compare the strings "A" and "B" using the less than operator, the result will indicate whether "A" is less than "B" based on their lexicographical order.

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<p>All conditional operators can be used on both numbers and strings.</p>
<p id="demo"></p>
<script>
let text1 = "A";
let text2 = "B";
let result = text1 < text2;
document.getElementById("demo").innerHTML = "Is A less than B? " + result;
</script>
</body>
</html>
```

Note that strings are compared alphabetically:

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<p>Note that strings are compared alphabetically:</p>
<p id="demo"></p>
<script>
let text1 = "20";
let text2 = "5";
let result = text1 < text2;
document.getElementById("demo").innerHTML = "Is 20 less than 5? " + result;
</script>
</body>
</html>
```

## JavaScript String Addition

In JavaScript, the ** +** operator is not only used for adding numbers but also for concatenating strings. By using

**, you can combine multiple strings into one:**

`+`

Example: Concatenating the strings "John" and "Doe" with a space in between results in the string "John Doe" when using the ** +** operator.

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<p>Note that strings are compared alphabetically:</p>
<p id="demo"></p>
<script>
let text1 = "20";
let text2 = "5";
let result = text1 < text2;
document.getElementById("demo").innerHTML = "Is 20 less than 5? " + result;
</script>
</body>
</html>
```

The ** +=** assignment operator in JavaScript can also be used for string concatenation. It appends the string on the right side of the operator to the string variable on the left side:

Example: Starting with the string "What a very ", and then using ** text1 += "nice day";** appends "nice day" to the existing string. This results in the combined string "What a very nice day".

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<h2>The += Operator</h2>
<p>The assignment operator += can concatenate strings.</p>
<p id="demo"></p>
<script>
let text1 = "What a very ";
text1 += "nice day";
document.getElementById("demo").innerHTML = text1;
</script>
</body>
</html>
```

## Adding Strings and Numbers

When you add two numbers in JavaScript, the result is their sum. However, adding a number and a string results in string concatenation because JavaScript converts the number into a string:

Example:

computes the sum of two numbers, which is`let x = 5 + 5;`

.`10`

combines a string and a number, resulting in the string`let y = "5" + 5;`

.`55`

concatenates a string with a number, producing`let z = "Hello" + 5;`

.`Hello5`

```
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Operators</h1>
<h2>The + Operator</h2>
<p>Adding a number and a string, returns a string.</p>
<p id="demo"></p>
<script>
let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;
document.getElementById("demo").innerHTML =
x + "<br>" + y + "<br>" + z;
</script>
</body>
</html>
```

## JavaScript Logical Operators

In JavaScript, logical operators are used to perform logical operations and typically return a boolean value based on the result of the operations. Here's a summary of the logical operators:

**&& (Logical AND)**: This operator returns true if both operands are true; otherwise, it returns false.**|| (Logical OR)**: It returns true if at least one of the operands is true; otherwise, it returns false.**! (Logical NOT)**: This operator inverts the truth value of its operand. If the operand is true, it returns false; if the operand is false, it returns true.

## JavaScript Type Operators

In JavaScript, type operators are useful for determining the type or the construction of variables and objects. Here’s a brief overview of these operators:

**typeof**: This operator returns the type of a variable as a string. It helps in identifying whether a variable is a number, string, function, or other types in JavaScript.**instanceof**: This operator checks whether an object is an instance of a particular class or constructor function. It returns true if the object inherits from the prototype property of the constructor.

## JavaScript Bitwise Operators

JavaScript bitwise operators manipulate individual bits within their operands, which are treated as 32-bit integers. Here's an overview of how these operators work along with their effects:

**& (AND)**: Compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, it is set to 0.- Example:
is`5 & 1`

because`1`

results in`0101 & 0001`

.`0001`

- Example:
**| (OR)**: Compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1.- Example:
is`5 | 1`

because`5`

results in`0101 | 0001`

.`0101`

- Example:
**~ (NOT)**: Inverts all the bits of its operand.- Example:
is not`~5`

in JavaScript, but`10`

because the bitwise NOT of`-6`

is`00000000000000000000000000000101`

.`11111111111111111111111111111010`

- Example:
**^ (XOR)**: Compares each bit of its first operand to the corresponding bit of its second operand. If the bits are different, the corresponding result bit is set to 1.- Example:
is`5 ^ 1`

because`4`

results in`0101 ^ 0001`

.`0100`

- Example:
**<< (Left Shift)**: Shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.- Example:
is`5 << 1`

because`10`

results in`0101 << 1`

.`1010`

- Example:
**>> (Right Shift)**: Shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.- Example:
is`5 >> 1`

because`2`

results in`0101 >> 1`

.`0010`

- Example:
**>>> (Unsigned Right Shift)**: Shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.- Example:
is`5 >>> 1`

because`2`

results in`0101 >>> 1`

.`0010`

- Example: