Javascript Operators and Expressions

Chapter-1:

JavaScript, operators and expressions are fundamental concepts that play a crucial role in building and manipulating values and data. Here’s an overview of operators and expressions in JavaScript:

Operators:
Operators are symbols that perform operations on values or variables. JavaScript provides a variety of operators for performing different types of operations.

1. Arithmetic Operators:
Arithmetic operators in JavaScript are symbols used to perform mathematical calculations on numeric values (operands). They allow you to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and more. Here’s a list of arithmetic operators in JavaScript along with examples:

Addition (+): Adds two values together.

let sum = 5 + 3; // sum is 8

Subtraction (-):  Subtracts the second value from the first.

let difference = 10 - 5; // difference is 5

Multiplication (*): Multiplies two values.

let product = 2 * 6; // product is 12

Division (/): Divides the first value by the second.

let quotient = 10 / 2; // quotient is 5

Modulus (%): Returns the remainder of the division operation.

let remainder = 7 % 3; // remainder is 1

Increment (++): Increases the value of a variable by 1.

let count = 5;
count++; // count is now 6

Decrement (–): Decreases the value of a variable by 1.

let quantity = 8;
quantity--; // quantity is now 7

Exponentiation () [ES6]**: Raises the first value to the power of the second.

let result = 2 ** 3; // result is 8 (2 raised to the power of 3)

Arithmetic operators follow the order of operations (PEMDAS/BODMAS), meaning operations inside parentheses are performed first, followed by exponentiation, multiplication, division, addition, and subtraction.

let result = 2 + 3 * 4; // result is 14, not 20 (3 * 4 is calculated first)
2. Assignment Operators:

Assignment operators in JavaScript are used to assign values to variables. They allow you to modify the value of a variable by assigning a new value or by performing an operation on the existing value. Here’s a list of assignment operators in JavaScript along with examples:

Assignment (=): Assigns a value to a variable.

let x = 10; // x is assigned the value 10

Addition Assignment (+=): Adds a value to the variable’s current value and assigns the result back to the variable.

let y = 5;
y += 3; // y is now 8 (5 + 3)

Subtraction Assignment (-=): Subtracts a value from the variable’s current value and assigns the result back to the variable.

let z = 7;
z -= 2; // z is now 5 (7 - 2)

Multiplication Assignment (*=): Multiplies the variable’s current value by a value and assigns the result back to the variable.

let a = 3;
a *= 4; // a is now 12 (3 * 4)

Division Assignment (/=): Divides the variable’s current value by a value and assigns the result back to the variable.

let b = 20;
b /= 5; // b is now 4 (20 / 5)

Modulus Assignment (%=): Calculates the remainder of dividing the variable’s current value by a value and assigns the result back to the variable.

let c = 15;
c %= 7; // c is now 1 (15 % 7)

Assignment operators are often used to update the value of a variable based on a calculation or modification. They can also be combined with other operators to perform more complex operations.

let total = 0;
total += 5; // total is now 5
total *= 2; // total is now 10

Keep in mind that assignment operators work from right to left. The operation on the right side is performed first, and then the result is assigned to the variable on the left side.

Also read, Javascript Data Types

3. Comparison Operators:
Comparison operators in JavaScript are used to compare two values and determine the relationship between them. These operators return a Boolean value (true or false) based on whether the comparison is true or false. Here’s a list of comparison operators in JavaScript along with examples:

Equal To (==): Checks if two values are equal, regardless of their data types (type coercion is applied).

let x = 5;
let y = '5';
console.log(x == y); // true (5 is equal to '5' after type coercion)

Not Equal To (!=): Checks if two values are not equal, regardless of their data types.

let a = 10;
let b = '15';
console.log(a != b); // true (10 is not equal to '15' after type coercion)

Strict Equal To (===): Checks if two values are equal and have the same data type (no type coercion is applied).

let p = 7;
let q = '7';
console.log(p === q); // false (7 and '7' have different types)

Strict Not Equal To (!==): Checks if two values are not equal or have different data types.

let m = true;
let n = 1;
console.log(m !== n); // true (true and 1 have different types)

Greater Than (>): Checks if the value on the left is greater than the value on the right.

let age = 18;
console.log(age > 16); // true (18 is greater than 16)

Less Than (<): Checks if the value on the left is less than the value on the right.

let score = 85;
console.log(score < 90); // true (85 is less than 90) Greater Than or Equal To (>=):

Checks if the value on the left is greater than or equal to the value on the right.

let price = 25;
console.log(price >= 20); // true (25 is greater than or equal to 20)

Less Than or Equal To (<=): Checks if the value on the left is less than or equal to the value on the right.

let quantity = 5;
console.log(quantity <= 5); // true (5 is less than or equal to 5)

Comparison operators are commonly used in conditional statements and loops to make decisions based on the comparison results. They allow you to evaluate and control the flow of your JavaScript code based on specific conditions.

Note: Note: In the upcoming chapter, we will delve into the details of pending operators and provide a comprehensive description.

Note: We welcome your feedback at Easy coding School. Please don’t hesitate to share your suggestions or any issues you might have with the article!

3 thoughts on “Javascript Operators and Expressions

Leave a Reply

Your email address will not be published. Required fields are marked *