# JavaScript: Step 2 – Operators

In JavaScript, operators are used to perform operations on variables and values. There are several types of operators in JavaScript, including:

1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Conditional (ternary) operator

Let’s take a closer look at each type of operator.

1. Arithmetic operators:

Example program:

```let x = 5;
let y = 2;

console.log(x + y); // Output: 7
console.log(x - y); // Output: 3
console.log(x * y); // Output: 10
console.log(x / y); // Output: 2.5
console.log(x % y); // Output: 1

```

2. Assignment operators:

Example program:

```let x = 5;

x += 2;
console.log(x); // Output: 7

x -= 2;
console.log(x); // Output: 5

x *= 2;
console.log(x); // Output: 10

x /= 2;
console.log(x); // Output: 5

x %= 2;
console.log(x); // Output: 1

```

3. Comparison operators:

Example program:

```console.log(5 == "5");   // Output: true
console.log(5 === "5");  // Output: false
console.log(5 != "5");   // Output: false
console.log(5 !== "5");  // Output: true
console.log(5 > 2);      // Output: true
console.log(5 < 2);      // Output: false
console.log(5 >= 5);     // Output: true
console.log(5 <= 2);     // Output: false

```

4. Logical operators:

Example program:

```let x = 5;
let y = 2;

console.log((x > 2) && (y < 1)); // Output: false
console.log((x > 2) || (y < 1)); // Output: true
console.log(!(x > 2)); // Output: false
console.log(!(y < 1)); // Output: true
```

5. Conditional (ternary) operator:

The ternary operator is a shorthand way of writing an if-else statement in a single line.

Example program:

```let x = 5;

console.log(x > 2 ? "x is greater than 2" : "x is less than or equal to 2");  // Output: "x is greater than 2"
```

This is equivalent to writing:

```if (x > 2) {
console.log("x is greater than 2");
} else {
console.log("x is less than or equal to 2");
}

```

Operators are used extensively in JavaScript to perform various tasks such as arithmetic calculations, logical evaluations, and assignment operations. By understanding the different types of operators and how they work, you can write more efficient and effective JavaScript code.