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 (+):
//javascriptlet sum = 5 + 10;
console.log(sum); // Output: 15
- Subtraction (-):
//javascriptlet difference = 15 - 5;
console.log(difference); // Output: 10
- Multiplication (*):
//javascriptlet product = 5 * 3;
console.log(product); // Output: 15
- Division (/):
//javascriptlet quotient = 15 / 3;
console.log(quotient); // Output: 5
- Modulus (%):
//javascriptlet remainder = 10 % 3;
console.log(remainder); // Output: 1
2. Comparison Operators
Comparison operators are used to compare values:
- Equal (==):
//javascriptconsole.log(5 == '5'); // Output: true
- Strict Equal (===):
//javascriptconsole.log(5 === '5'); // Output: false
- Not Equal (!=):
//javascriptconsole.log(5 != '5'); // Output: false
- Strict Not Equal (!==):
//javascriptconsole.log(5 !== '5'); // Output: true
- Greater Than (>):
//javascriptconsole.log(10 > 5); // Output: true
- Less Than (<):
//javascriptconsole.log(10 < 5); // Output: false
- Greater Than or Equal (>=):
//javascriptconsole.log(10 >= 5); // Output: true
- Less Than or Equal (<=):
//javascriptconsole.log(10 <= 5); // Output: false
3. Logical Operators
Logical operators are used to combine multiple conditions:
- AND (&&):
//javascriptlet result = (5 > 3 && 10 > 5);
console.log(result); // Output: true
- OR (||):
//javascriptlet result = (5 > 3 || 10 < 5);
console.log(result); // Output: true
- NOT (!):
//javascriptlet result = !(5 > 3);
console.log(result); // Output: false
4. Assignment Operators
Assignment operators are used to assign values to variables:
- Simple Assignment (=):
//javascriptlet x = 10;
- Add and Assign (+=):
//javascriptx += 5; // Equivalent to x = x + 5
- Subtract and Assign (-=):
//javascriptx -= 3; // Equivalent to x = x - 3
- Multiply and Assign (*=):
//javascriptx *= 2; // Equivalent to x = x * 2
- Divide and Assign (/=):
//javascriptx /= 2; // Equivalent to x = x / 2
5. Examples and Exercises
Let’s practice using operators with a few examples:
- Calculating Area of a Rectangle:
//javascriptlet length = 10;
let width = 5;
let area = length * width;
console.log('Area of the rectangle:', area); // Output: 50
- Comparing Ages:
//javascriptlet 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.