Learning Operators | Control Flow | Objects. Drop your notes and doubts here.

```
if ((a * 5) <= b+10 ){
console.log("a is greater than b");
} else {
console.log("b is greater than a");
}
```

```
let a = 20;
let b = 10;
if (2 * (a * 5) + 10 <= b * (10 + 10)) {
console.log("if --->");
} else {
console.log("else -->");
}
```

Write an explanation for this one

24/05/2023 -CLASS NOTES

Operators

JavaScript provides a variety of operators that allow you to perform different operations on values. These operators can be classified into several categories, including arithmetic operators, assignment operators, comparison operators, logical operators, bitwise operators, and more. Hereâ€™s an overview of some commonly used operators in JavaScript:

Airthmetic,Relational and Logical

1.Arithmetic Operators:

A typical arithmetic operation operates on two numbers.Types of airthmetic operators.

Addition: +

Subtraction: -

Multiplication: *

Division: /

Remainder(modulo): %

Power: **

The two numbers can be literals:

Example

let x = 100 + 50;

or variables:

Example

let x = a + b;

or expressions:

Example

let x = (100 + 50) * a;

Operators and Operands

The numbers (in an arithmetic operation) are called operands.

The operation (to be performed between the two operands) is defined by an operator.

Operand Operator Operand

100 + 50

Adding

The addition operator (+) adds numbers:

Example

let x = 5;

let y = 2;

let z = x + y;

Subtracting

The subtraction operator (-) subtracts numbers.

Example

let x = 5;

let y = 2;

let z = x - y;

Multiplying

The multiplication operator (*) multiplies numbers.

Example

let x = 5;

let y = 2;

let z = x * y;

Dividing

The division operator (/) divides numbers.

Example

let x = 5;

let y = 2;

let z = x / y;

Remainder

The modulus operator (%) returns the division remainder.

Example

let x = 5;

let y = 2;

let z = x % y;

In arithmetic, the division of two integers produces a quotient and a remainder.

In mathematics, the result of a modulo operation is the remainder of an arithmetic division.

Exponentiation

The exponentiation operator (**) raises the first operand to the power of the second operand.

Example

let x = 5;

let z = x ** 2;

x ** y produces the same result as Math.pow(x,y):

Example

let x = 5;

let z = Math.pow(x,2);

Operator Precedence

Operator precedence describes the order in which operations are performed in an arithmetic expression.

Example

let x = 100 + 50 * 3;

Is the result of example above the same as 150 * 3, or is it the same as 100 + 150?

Is the addition or the multiplication done first?

As in traditional school mathematics, the multiplication is done first.

Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).

And (as in school mathematics) the precedence can be changed by using parentheses.

When using parentheses, the operations inside the parentheses are computed first:

Example

let x = (100 + 50) * 3;

When many operations have the same precedence (like addition and subtraction or multiplication and division), they are computed from left to right:

Examples

let x = 100 + 50 - 3;

let x = 100 / 50 * 3;

2.Assignment Operators:

Assignment: =

Addition assignment: +=

Subtraction assignment: -=

Multiplication assignment: *=

Division assignment: /=

Remainder assignment: %=

The = Operator

The Simple Assignment Operator assigns a value to a variable.

Simple Assignment Examples

let x = 10;

let x = 10 + y;

The += Operator

The Addition Assignment Operator adds a value to a variable.

Addition Assignment Examples

let x = 10;

x += 5;

let text = â€śHelloâ€ť; text += " World";

The -= Operator

The Subtraction Assignment Operator subtracts a value from a variable.

Subtraction Assignment Example

let x = 10;

x -= 5;

The *= Operator

The Multiplication Assignment Operator multiplies a variable.

Multiplication Assignment Example

let x = 10;

x *= 5;

The **= Operator

The Exponentiation Assignment Operator raises a variable to the power of the operand.

Exponentiation Assignment Example

let x = 10;

x **= 5;

The /= Operator

The Division Assignment Operator divides a variable.

Division Assignment Example

let x = 10;

x /= 5;

The %= Operator

The Remainder Assignment Operator assigns a remainder to a variable.

Remainder Assignment Example

let x = 10;

x %= 5;

3.Comparison Operators: Relational

Relational operators are operators that check whether a relationship between two values (like â€śless-thanâ€ť or â€śproperty-ofâ€ť) exists and deliver true or false depending on the result.

Equality: ==

Greater than: >

Less than: <

Greater than or equal to: >=

Less than or equal to: <=

1.Equal to (==) operator: It compares two values for equality, allowing for type coercion.

Example

console.log(5 == 5); // true

console.log(â€ś5â€ť == 5); // true (coerces string to a number)

console.log(5 == â€ś5â€ť); // true (coerces string to a number)

console.log(5 == 6); // false

2.Not equal to (!=) operator: It checks if two values are not equal, allowing for type coercion.

Example:

console.log(5 != 6); // true

console.log(5 != â€ś5â€ť); // false (coerces string to a number)

console.log(5 != 5); // false

Strict equal to (===) operator: It compares two values for equality without type coercion. Both value and type must match.

Example:

console.log(5 === 5); // true

console.log(â€ś5â€ť === 5); // false (different types)

console.log(5 === â€ś5â€ť); // false (different types)

console.log(5 === 6); // false

Strict not equal to (!==) operator: It checks if two values are not equal without type coercion. Both value and type must be different.

Example:

console.log(5 !== 6); // true

console.log(5 !== â€ś5â€ť); // true (different types)

console.log(5 !== 5); // false

Greater than (>) operator: It checks if the value on the left is greater than the value on the right.

Example:

console.log(5 > 2); // true

console.log(2 > 5); // false

console.log(5 > 5); // false

Less than (<) operator: It checks if the value on the left is less than the value on the right.

Example:

console.log(2 < 5); // true

console.log(5 < 2); // false

console.log(5 < 5); // false

Greater than or equal to (>=) operator: It checks if the value on the left is greater than or equal to the value on the right.

Example

console.log(5 >= 2); // true

console.log(5 >= 5); // true

console.log(2 >= 5); // false

Less than or equal to (<=) operator: It checks if the value on the left is less than or equal to the value on the right.

Example

console.log(2 <= 5); // true

console.log(5 <= 5); // true

console.log(5 <= 2); // false

These are the relational operators in JavaScript along with examples. They are useful for making comparisons in conditional statements and determining the flow of the program based on the evaluation of these comparisons.

4.Logical Operators:

Logical AND: &&

Logical OR: ||

Logical NOT: !

In JavaScript, logical operators are used to combine or modify boolean values and return a boolean result. There are three logical operators: && (AND), || (OR), and ! (NOT). Here are the logical operators in JavaScript along with examples:

Logical AND (&&) operator: It returns true if both operands are true, otherwise, it returns false.

Example:

console.log(true && true); // true

console.log(true && false); // false

console.log(false && true); // false

console.log(false && false); // false

Logical OR (||) operator: It returns true if either of the operands is true, otherwise, it returns false.

Example:

console.log(true || true); // true

console.log(true || false); // true

console.log(false || true); // true

console.log(false || false); // false

Logical NOT (!) operator: It negates the boolean value, returning true if the operand is false, and false if the operand is true.

Example:

console.log(!true); // false

console.log(!false); // true

Control Flow

In JavaScript, control flow refers to the order in which statements are executed in a program. It allows you to control the flow of execution based on different conditions and make decisions using conditional statements. Here are some examples of control flow structures in JavaScript:

if statement: It executes a block of code if a specified condition is true.

Example:

var age = 18;

if (age >= 18) {

console.log(â€śYou are eligible to vote.â€ť);

} else {

console.log(â€śYou are not eligible to vote yet.â€ť);

}

ifâ€¦else statement: It executes a block of code if a specified condition is true, otherwise, it executes another block of code.

Example:

var num = 7;

if (num % 2 === 0) {

console.log(â€śThe number is even.â€ť);

} else {

console.log(â€śThe number is odd.â€ť);

}

ifâ€¦else ifâ€¦else statement: It allows you to test multiple conditions and execute different blocks of code based on those conditions.

Example:

var day = â€śWednesdayâ€ť;

if (day === â€śMondayâ€ť) {

console.log(â€śToday is Monday.â€ť);

} else if (day === â€śTuesdayâ€ť) {

console.log(â€śToday is Tuesday.â€ť);

} else if (day === â€śWednesdayâ€ť) {

console.log(â€śToday is Wednesday.â€ť);

} else {

console.log(â€śItâ€™s another day of the week.â€ť);

}

The `if`

statement checks a condition:

2 * (a * 5) + 10 <= b * (10 + 10)

Evaluation of the statement:

- The expression
`a * 5`

evaluates to 20 * 5 = 100. - The expression
`b * (10 + 10)`

evaluates to 10 * (10 + 10) = 10 * 20 = 200. - Multiplying the previous result by 2, we get 2 * 200 = 400.
- Finally, adding 10 to this result gives us 400 + 10 = 410.

Now the condition becomes:

2 * (a * 5) + 10 <= b * (10 + 10)

2 * 100 + 10 <= 10 * 20

210 <= 200

Since 210 is not less than or equal to 200, the condition evaluates to `false`

.

So, the code will execute the `else`

block. The statement `console.log("else -->");`

will be executed, and it will print â€śelse -->â€ť to the console.

the `if`

statement checks the condition:

(a * 5) <= b + 10

Evaluation

1.The expression `a * 5`

evaluates to 20 * 5 = 100.

2.The expression `b + 10`

evaluates to 10 + 10 = 20.

Therefore, the condition becomes:

(a * 5) <= b + 10

100 <= 20

100 is not less than or equal to 20, the condition evaluates to `false`

.

So,the code will execute the `else`

block. The statement `console.log("b is greater than a");`

will be executed, and it will print â€śb is greater than aâ€ť to the console.