Now that we've touched on variables and data types, let's dive into the fundamental actions our algorithms can perform: basic operations. These are the workhorses of computation, allowing us to manipulate data and make decisions. Think of them as the verbs in our algorithmic language.
The most common operations revolve around numbers, and these are often referred to as arithmetic operations. They allow us to perform calculations, which are central to many algorithmic tasks.
Here are the core arithmetic operations you'll encounter:
Addition: This is straightforward – combining two numbers to get their sum. In most programming languages, it's represented by the '+' symbol.
let sum = 10 + 5;
console.log(sum); // Output: 15Subtraction: Finding the difference between two numbers. The symbol for subtraction is typically '-'.
let difference = 20 - 7;
console.log(difference); // Output: 13Multiplication: Calculating the product of two numbers. The '*' symbol is commonly used for this.
let product = 6 * 4;
console.log(product); // Output: 24Division: Splitting a number by another to find the quotient. The '/' symbol represents division.
let quotient = 30 / 5;
console.log(quotient); // Output: 6Modulo (or Remainder): This operation gives you the remainder after division. It's incredibly useful for tasks like determining if a number is even or odd, or for cyclical operations. It's often represented by the '%' symbol.