Mastering JavaScript Operators and Expressions : Day-4

Mastering JavaScript Operators and Expressions

Introduction

Introduction (Mastering JavaScript Operators and Expressions)

JavaScript Operators and expressions are building blocks of any programming language. In JavaScript, operators allow you to perform various operations on data, while expressions combine these operations to produce values.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations:

  • Addition (+):
//javascript
let sum = 5 + 10;
console.log(sum); // Output: 15
  • Subtraction (-):
//javascript
let difference = 15 - 5;
console.log(difference); // Output: 10
  • Multiplication (*):
//javascript
let product = 5 * 3;
console.log(product); // Output: 15
  • Division (/):
//javascript
let quotient = 15 / 3;
console.log(quotient); // Output: 5
  • Modulus (%):
//javascript
let remainder = 10 % 3;
console.log(remainder); // Output: 1

2. Comparison Operators

Comparison operators are used to compare values:

  • Equal (==):
//javascript
console.log(5 == '5'); // Output: true
  • Strict Equal (===):
//javascript
console.log(5 === '5'); // Output: false
  • Not Equal (!=):
//javascript
console.log(5 != '5'); // Output: false
  • Strict Not Equal (!==):
//javascript
console.log(5 !== '5'); // Output: true
  • Greater Than (>):
//javascript
console.log(10 > 5); // Output: true
  • Less Than (<):
//javascript
console.log(10 < 5); // Output: false
  • Greater Than or Equal (>=):
//javascript
console.log(10 >= 5); // Output: true
  • Less Than or Equal (<=):
//javascript
console.log(10 <= 5); // Output: false

3. Logical Operators

Logical operators are used to combine multiple conditions:

  • AND (&&):
//javascript
let result = (5 > 3 && 10 > 5);
console.log(result); // Output: true
  • OR (||):
//javascript
let result = (5 > 3 || 10 < 5);
console.log(result); // Output: true
  • NOT (!):
//javascript
let result = !(5 > 3);
console.log(result); // Output: false

4. Assignment Operators

Assignment operators are used to assign values to variables:

  • Simple Assignment (=):
//javascript
let x = 10;
  • Add and Assign (+=):
//javascript
x += 5; // Equivalent to x = x + 5
  • Subtract and Assign (-=):
//javascript
x -= 3; // Equivalent to x = x - 3
  • Multiply and Assign (*=):
//javascript
x *= 2; // Equivalent to x = x * 2
  • Divide and Assign (/=):
//javascript
x /= 2; // Equivalent to x = x / 2

5. Examples and Exercises

Let’s practice using operators with a few examples:

  • Calculating Area of a Rectangle:
//javascript
let length = 10;
let width = 5;
let area = length * width;
console.log('Area of the rectangle:', area); // Output: 50
  • Comparing Ages:
//javascript
let age1 = 25;
let age2 = 30;
let isOlder = age1 > age2;
console.log('Is age1 older than age2?', isOlder); // Output: false

Conclusion

Mastering operators and expressions is crucial for performing calculations, making decisions, and writing efficient code in JavaScript. With these fundamentals, you can tackle more complex programming challenges and build dynamic applications.

If you haven’t read our last day blog on the basic introduction to JavaScript, click here to read our Blogs. want to connect me on linkdin.

About The Author

Leave a Comment

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