In this section, we'll cover the fundamental operators in JavaScript that allow you to perform various operations on data. Understanding these operators is crucial as they form the building blocks for more complex expressions and logic in your programs.
- Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 6 / 3 |
2 |
% |
Modulus (Remainder) | 5 % 3 |
2 |
++ |
Increment | let a = 5; a++ |
6 |
-- |
Decrement | let a = 5; a-- |
4 |
Example:
let a = 10; let b = 3; console.log(a + b); // 13 console.log(a - b); // 7 console.log(a * b); // 30 console.log(a / b); // 3.3333333333333335 console.log(a % b); // 1 a++; console.log(a); // 11 b--; console.log(b); // 2
- Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example | Equivalent To |
---|---|---|---|
= |
Assignment | a = 5 |
a = 5 |
+= |
Addition assignment | a += 3 |
a = a + 3 |
-= |
Subtraction assignment | a -= 3 |
a = a - 3 |
*= |
Multiplication assignment | a *= 3 |
a = a * 3 |
/= |
Division assignment | a /= 3 |
a = a / 3 |
%= |
Modulus assignment | a %= 3 |
a = a % 3 |
Example:
let a = 10; a += 5; console.log(a); // 15 a -= 3; console.log(a); // 12 a *= 2; console.log(a); // 24 a /= 4; console.log(a); // 6 a %= 4; console.log(a); // 2
- Comparison Operators
Comparison operators are used to compare two values and return a boolean value (true
or false
).
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | 5 == 5 |
true |
=== |
Strict equal to | 5 === '5' |
false |
!= |
Not equal to | 5 != 3 |
true |
!== |
Strict not equal to | 5 !== '5' |
true |
> |
Greater than | 5 > 3 |
true |
< |
Less than | 5 < 3 |
false |
>= |
Greater than or equal to | 5 >= 5 |
true |
<= |
Less than or equal to | 5 <= 3 |
false |
Example:
let a = 5; let b = '5'; console.log(a == b); // true console.log(a === b); // false console.log(a != b); // false console.log(a !== b); // true console.log(a > 3); // true console.log(a < 3); // false console.log(a >= 5); // true console.log(a <= 3); // false
- Logical Operators
Logical operators are used to combine multiple boolean expressions.
Operator | Description | Example | Result |
---|---|---|---|
&& |
Logical AND | true && false |
false |
` | ` | Logical OR | |
! |
Logical NOT | !true |
false |
Example:
let a = true; let b = false; console.log(a && b); // false console.log(a || b); // true console.log(!a); // false
- Practical Exercises
Exercise 1: Arithmetic Operations
Write a program that calculates the sum, difference, product, and quotient of two numbers.
Solution:
let num1 = 8; let num2 = 4; let sum = num1 + num2; let difference = num1 - num2; let product = num1 * num2; let quotient = num1 / num2; console.log('Sum:', sum); // Sum: 12 console.log('Difference:', difference); // Difference: 4 console.log('Product:', product); // Product: 32 console.log('Quotient:', quotient); // Quotient: 2
Exercise 2: Comparison and Logical Operations
Write a program that compares two numbers and prints whether the first number is greater than, less than, or equal to the second number. Also, use logical operators to check if both numbers are positive.
Solution:
let num1 = 5; let num2 = 10; if (num1 > num2) { console.log('num1 is greater than num2'); } else if (num1 < num2) { console.log('num1 is less than num2'); } else { console.log('num1 is equal to num2'); } if (num1 > 0 && num2 > 0) { console.log('Both numbers are positive'); } else { console.log('One or both numbers are not positive'); }
Conclusion
In this section, we covered the basic operators in JavaScript, including arithmetic, assignment, comparison, and logical operators. These operators are essential for performing various operations and making decisions in your programs. Make sure to practice using these operators to become comfortable with them, as they will be used frequently in more complex programming tasks.
JavaScript: From Beginner to Advanced
Module 1: Introduction to JavaScript
- What is JavaScript?
- Setting Up Your Development Environment
- Your First JavaScript Program
- JavaScript Syntax and Basics
- Variables and Data Types
- Basic Operators
Module 2: Control Structures
Module 3: Functions
- Defining and Calling Functions
- Function Expressions and Arrow Functions
- Parameters and Return Values
- Scope and Closures
- Higher-Order Functions
Module 4: Objects and Arrays
- Introduction to Objects
- Object Methods and 'this' Keyword
- Arrays: Basics and Methods
- Iterating Over Arrays
- Array Destructuring
Module 5: Advanced Objects and Functions
- Prototypes and Inheritance
- Classes and Object-Oriented Programming
- Modules and Import/Export
- Asynchronous JavaScript: Callbacks
- Promises and Async/Await
Module 6: The Document Object Model (DOM)
- Introduction to the DOM
- Selecting and Manipulating DOM Elements
- Event Handling
- Creating and Removing DOM Elements
- Form Handling and Validation
Module 7: Browser APIs and Advanced Topics
- Local Storage and Session Storage
- Fetch API and AJAX
- WebSockets
- Service Workers and Progressive Web Apps (PWAs)
- Introduction to WebAssembly
Module 8: Testing and Debugging
Module 9: Performance and Optimization
- Optimizing JavaScript Performance
- Memory Management
- Efficient DOM Manipulation
- Lazy Loading and Code Splitting
Module 10: JavaScript Frameworks and Libraries
- Introduction to React
- State Management with Redux
- Vue.js Basics
- Angular Basics
- Choosing the Right Framework