JavaScript, like most programming languages, includes a variety of operators that perform operations on data values. Here are some of the basic operators you'll frequently encounter in JavaScript:
- Arithmetic Operators: Arithmetic operators are used to perform basic mathematical operations. Here are the most used arithmetic operators in JavaScript:
Addition (+): Adds two operands.
let a = 5; let b = 3; let sum = a + b; console.log('Addition:', sum); //Output: 8
Subtraction (-): Subtracts the right operand from the left operand.
let a = 5; let b = 3; let subtraction = a - b; console.log('Subtraction:', subtraction); //Output: 2
Multiplication (*): Multiplies two operands.
let a = 10; let b = 2; let multiplication = a * b; console.log("Multiplication: ", multiplication) //Output: 20
Division (/): Divides the left operand by the right operand.
let a = 12; let b = 6; let division = a / b; console.log("Division: ", division) //Output: 2
Exponentiation (**): Raises one number to the power of another.
let power = 2 ** 3; console.log('Exponentiation:', power); // 8
Increment (++): Increases a number by 1.
let c = 5; c++; // c = c + 1 console.log('Increment:', c); // 6
Decrement (--): Decreases a number by 1.
let c = 5; c--; // c = c - 1 console.log('Decrement :', c); // 4
Assignment Operators: Assignment operators are used to assign values to variables.
Assignment (=): Assigns the right operand to the left operand.
let ageOfAdam = 28; "It assigns the value 28 to the variable ageOfAdam" console.log(ageOfAdam) //Output: 28
Addition Assignment (+=): Adds and assigns the value
let x = 10 + 5; console.log(x) //Output: 15 x = x + 10; console.log(x) //Output: 25 "Now Instead of using the long way to add any value to the variable, we can use addition assignment, which uses the same logic." x += 10; console.log(x) //Output: 35
Subtraction Assignment (-=): Subtracts and assigns the value to the variable.
let x = 20 + 5; console.log(x) //Output: 25 x = x - 10; console.log(x) //Output: 15 "Now Instead of using the long way to subtract any value from the variable, we can use subtraction assignment, which uses the same logic." x -= 10; console.log(x) //Output: 5
Multiplication Assignment (*=): Multiplies and assigns the value to the variable.
let x = 20 + 5; console.log(x) //Output: 25 x = x * 10; console.log(x) //Output: 250 "Now Instead of using the long way to multiply any value with the variable, we can use multiplication assignment, which uses the same logic." x *= 10; console.log(x) //Output: 2500
Division Assignment (/=): Divides and assigns the value to the variable.
let x = 20 + 5; console.log(x) //Output: 25 x = x / 10; console.log(x) //Output: 2.5 "Now Instead of using the long way to divide any value, we can use division assignment, which uses the same logic." x /= 10; console.log(x) //Output: 0.25
Comparison Operators: Comparison operators are used to compare two values. They return boolean values which are either True or False.
Equal to (==): Checks if two values are equal.
let ageOfAdam = 25; let ageOfJulia = 24; let isEqual = (ageOfAdam == ageOfJulia); console.log(isEqual); // false
Not equal to (!=): Checks if two values are not equal.
let ageOfAdam = 25; let ageOfJulia = 24; let isNotEqual = (ageOfAdam != ageOfJulia); console.log(isNotEqual); // true
Greater than (>): Checks if one value is greater than another.
let ageOfAdam = 25; let ageOfJulia = 24; let isGreater = (ageOfAdam > ageOfJulia); console.log(isGreater); // true
Greater than or equal to (>=): Checks if one value is greater than or equal to another.
let ageOfJulia = 18; let legalAgeToDrive = 18 let isGreaterOrEqual = (ageOfJulia >= legalAgeToDrive); console.log(isGreater); // true
Less than (<): Checks if one value is less than another.
let ageOfAdam = 25; let ageOfJulia = 24; let isLess = (ageOfAdam < ageOfJulia); console.log(isLess); // false
Less than or equal to (<=): Checks if one value is less than or equal to another.
let ageOfAdam = 17; let legalAgeToDrive = 18; let isLessOrEqual = (ageOfAdam <= legalAgeToDrive); console.log(isLessOrEqual); // true
Conclusion:
These basic operators are the building blocks of JavaScript. By understanding how to use them, you'll be able to write more complex and useful code. Happy coding!