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.

  1. 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

  1. 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

  1. 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

  1. 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

  1. Practical Exercises

Exercise 1: Arithmetic Operations

Write a program that calculates the sum, difference, product, and quotient of two numbers.

let num1 = 8;
let num2 = 4;

// Your code here

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.

let num1 = 5;
let num2 = 10;

// Your code here

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

Module 2: Control Structures

Module 3: Functions

Module 4: Objects and Arrays

Module 5: Advanced Objects and Functions

Module 6: The Document Object Model (DOM)

Module 7: Browser APIs and Advanced Topics

Module 8: Testing and Debugging

Module 9: Performance and Optimization

Module 10: JavaScript Frameworks and Libraries

Module 11: Final Project

© Copyright 2024. All rights reserved