Lecture Notes of Day 4: Operators in JavaScript

Rashmi Mishra
0

 

Lecture Notes of Day 4

Operators in JavaScript

Objective: 

Understand operators and their usage.


Introduction

In JavaScript, operators are special symbols used to perform operations on variables and values. Operators are essential in almost every aspect of programming, including mathematical operations, comparison, logical decisions, and assignment of values.

In this lesson, we will cover several important categories of operators:

1.   Arithmetic Operators

2.   Comparison Operators

3.   Logical Operators

4.   Assignment Operators

Each category of operators has specific purposes and is used to perform different tasks in JavaScript. 


1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus (remainder of division).

Operators in JavaScript:

Operator

Description

Example

Result

+

Addition

3 + 4

7

-

Subtraction

5 - 2

3

*

Multiplication

6 * 3

18

/

Division

10 / 2

5

%

Modulus (remainder)

10 % 3

1

Examples:

1.   Addition:

let sum = 3 + 4;   // sum will be 7

console.log(sum);

2.   Subtraction:

let difference = 5 - 2;   // difference will be 3

console.log(difference);

3.   Multiplication:

let product = 6 * 3;   // product will be 18

console.log(product);

4.   Division:

let quotient = 10 / 2;   // quotient will be 5

console.log(quotient);

5.   Modulus:

let remainder = 10 % 3;   // remainder will be 1 (because 10 divided by 3 gives a remainder of 1)

console.log(remainder);


2. Comparison Operators

Comparison operators are used to compare two values. These operators return a boolean value (true or false) based on the condition being evaluated.

Operators in JavaScript:

Operator

Description

Example

Result

==

Equal to (value comparison)

5 == '5'

true

===

Equal to (value and type)

5 === '5'

false

!=

Not equal to

5 != 3

true

Greater than

5 > 3

true

Less than

3 < 5

true

>=

Greater than or equal to

5 >= 5

true

<=

Less than or equal to

3 <= 5

true

Examples:

1.   Equal to:

let isEqual = 5 == '5';  // true, because values are the same

console.log(isEqual);

2.   Strict Equal to (checks both value and type):

let isStrictEqual = 5 === '5';  // false, because they are of different types (number vs string)

console.log(isStrictEqual);

3.   Not Equal to:

let isNotEqual = 5 != 3;   // true, because 5 is not equal to 3

console.log(isNotEqual);

4.   Greater than:

let isGreater = 5 > 3;   // true, because 5 is greater than 3

console.log(isGreater);

5.   Less than:

let isLess = 3 < 5;      // true, because 3 is less than 5

console.log(isLess);

6.   Greater than or equal to:

let isGreaterOrEqual = 5 >= 5;   // true, because 5 is equal to 5

console.log(isGreaterOrEqual);

7.   Less than or equal to:

let isLessOrEqual = 3 <= 5;  // true, because 3 is less than 5

console.log(isLessOrEqual);


3. Logical Operators

Logical operators are used to combine multiple conditions and return a boolean value (true or false).

Operators in JavaScript:

Operator

Description

Example

Result

&&

Logical AND

true && false

false

`

`

Logical OR

!

Logical NOT

!true

false

Examples:

1.   Logical AND (&&):

let resultAnd = true && false;  // false, because both conditions must be true for the result to be true

console.log(resultAnd);

2.   Logical OR (||):

let resultOr = true || false;   // true, because only one condition needs to be true for the result to be true

console.log(resultOr);

3.   Logical NOT (!):

let resultNot = !true;   // false, negates the value of true

console.log(resultNot);


4. Assignment Operators

Assignment operators are used to assign values to variables. In addition to the simple assignment, there are shorthand operators that combine assignment with other operations.

Operators in JavaScript:

Operator

Description

Example

Result

=

Simple assignment

x = 5

Assign 5 to x

+=

Add and assign

x += 3

x = x + 3

-=

Subtract and assign

x -= 2

x = x - 2

*=

Multiply and assign

x *= 2

x = x * 2

/=

Divide and assign

x /= 2

x = x / 2

Examples:

1.   Simple Assignment:

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

console.log(x);

2.   Add and Assign:

let y = 3;

y += 2;   // Equivalent to y = y + 2

console.log(y);  // Output will be 5

3.   Subtract and Assign:

let z = 7;

z -= 3;   // Equivalent to z = z - 3

console.log(z);  // Output will be 4

4.   Multiply and Assign:

let a = 4;

a *= 3;   // Equivalent to a = a * 3

console.log(a);  // Output will be 12

5.   Divide and Assign:

let b = 10;

b /= 2;   // Equivalent to b = b / 2

console.log(b);  // Output will be 5


Conclusion

Operators are the building blocks for performing operations in JavaScript. Understanding the various types of operators and their syntax is crucial for writing efficient and functional code. In this lesson, we have learned about arithmetic, comparison, logical, and assignment operators.

Remember that operators are not just used for calculations but are essential for making decisions (through comparison and logical operators) and manipulating data (through assignment operators).


Practice

1.   Use arithmetic operators to create an equation with the result 100.

2.   Compare two values using comparison operators and print the results to the console.

3.   Use logical operators to check if both conditions are true or either is true.

4.   Use assignment operators to simplify variable updates (e.g., increasing a score by 10 each time).


 


Post a Comment

0Comments

Post a Comment (0)