js operators

JavaScript Operators


šŸŽÆ Assignment Operator (=)

āœ–ļø JavaScript Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations between variables and values.

Examples: Given y = 5, the table below explains arithmetic operators.





šŸ“¦ Arithmetic Operators Table šŸ“¦

| Operator | Description        | Example    | Result  |
|----------|--------------------|------------|---------|
| `+`      | Addition           | x = y + 2  | x = 7   |
| `-`      | Subtraction        | x = y - 2  | x = 3   |
| `*`      | Multiplication     | x = y * 2  | x = 10  |
| `/`      | Division           | x = y / 2  | x = 2.5 |
| `%`      | Modulus (Remainder)| x = y % 2  | x = 1   |
| `++`     | Increment          | x = ++y    | x = 6   |
| `--`     | Decrement          | x = --y    | x = 4   |

šŸŽ›ļø **JavaScript Assignment Operators**

Assignment operators are used to assign values to JavaScript variables.

Examples:
Given x = 10 and y = 5,
 the table below explains assignment operators.


šŸ“¦ Assignment Operators Table šŸ“¦

| Operator | Example  | Same As     | Result  |
|----------|---------|-------------|---------|
| `=`      | x = y   | x = y       | x = 5   |
| `+=`     | x += y  | x = x + y   | x = 15  |
| `-=`     | x -= y  | x = x - y   | x = 5   |
| `*=`     | x *= y  | x = x * y   | x = 50  |
| `/=`     | x /= y  | x = x / y   | x = 2   |
| `%=`     | x %= y  | x = x % y   | x = 0   |


āœļø **Using the + Operator with Strings**

The + operator can also be used to concatenate strings.

Example 1: javascript let txt1 = "What a very"; let txt2 = "nice day"; let txt3 = txt1 + txt2;

console.log(txt3); // Output: "What a verynice day"



Adding Space Between Strings:

javascript let txt1 = "What a very "; let txt2 = "nice day"; let txt3 = txt1 + txt2; console.log(txt3); // Output: "What a very nice day" Insert Space Directly in the Expression:

javascript let txt3 = txt1 + " " + txt2;

console.log(txt3); // Output: "What a very nice day"




šŸ”¢ Adding Strings and Numbers

If a number and a string are added together, the result will be a string.

Examples: javascript let x = 5 + 5; console.log(x); // Output: 10

let y = "5" + "5"; console.log(y); // Output: "55"

let z = 5 + "5"; console.log(z); // Output: "55"

let w = "5" + 5; console.log(w); // Output: "55"


Rule:
šŸ“Œ If a number and a string are added together, the result will always be a string.
šŸ› ļø **JavaScript Comparison and Logical Operators**

Comparison Operators:

Used in logical statements to compare values.

Given x = 5, the table below explains comparison operators:
šŸ“¦ Comparison Operators Table šŸ“¦

| Operator | Description                          | Example       | Result  |
|----------|--------------------------------------|--------------|---------|
| `==`     | Equal to                            | x == 8       | false   |
| `===`    | Strictly equal (value & type)      | x === 5      | true    |
|          |                                     | x === "5"    | false   |
| `!=`     | Not equal to                        | x != 8       | true    |
| `>`      | Greater than                        | x > 8        | false   |
| `<`      | Less than                           | x < 8        | true    |
| `>=`     | Greater than or equal to           | x >= 8       | false   |
| `<=`     | Less than or equal to              | x <= 8       | true    |


Usage Example:
javascript
if (age < 18) {

    console.log("Too young");
}

**Logical Operators:**

Logical operators determine the logic between variables or values.

Given x = 6 and y = 3, the table below explains logical operators:



šŸ“¦ Logical Operators Table šŸ“¦

| Operator | Description  | Example                 | Result  |
|----------|-------------|-------------------------|---------|
| `&&`     | AND         | (x < 10 && y > 1)       | true    |
| `||`     | OR          | (x > 10 || y < 5)       | true    |
| `!`      | NOT         | !(x == y)               | true    |

šŸŽÆ **JavaScript Conditional (Ternary) Operator**

The ternary operator assigns a value to a variable based on a condition.

Syntax:
javascript
variablename = (condition) ? value1 : value2;

Example:
javascript
let visitor = "PRES";
let greeting = (visitor == "PRES") ? "Dear President " : "Dear ";

console.log(greeting); // Output: "Dear President "


Explanation:

If visitor is "PRES", greeting will be "Dear President ". Otherwise, it will be "Dear".

Quiz

To mark this module as complete, you must finish this quiz. Once submitted, you'll need to wait 2 hours before attempting it again.