Basic Operators in JavaScript
A deep dive into how operators work in JavaScript

Greetings! I'm Magnifiques 📿, a web developer and UI/UX designer passionate about creating amazing digital experiences. I specialize in ReactJS and JavaScript, and my projects highlight my expertise with the ReactJS library. I also use Node.js and Express.js for backend development and Figma for designing user-friendly interfaces. I'm committed to delivering top-quality solutions that exceed user expectations.
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: 8Subtraction (-): Subtracts the right operand from the left operand.
let a = 5; let b = 3; let subtraction = a - b; console.log('Subtraction:', subtraction); //Output: 2Multiplication (*): Multiplies two operands.
let a = 10; let b = 2; let multiplication = a * b; console.log("Multiplication: ", multiplication) //Output: 20Division (/): Divides the left operand by the right operand.
let a = 12; let b = 6; let division = a / b; console.log("Division: ", division) //Output: 2Exponentiation (**): Raises one number to the power of another.
let power = 2 ** 3; console.log('Exponentiation:', power); // 8Increment (++): Increases a number by 1.
let c = 5; c++; // c = c + 1 console.log('Increment:', c); // 6Decrement (--): 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: 28Addition 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: 35Subtraction 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: 5Multiplication 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: 2500Division 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); // falseNot equal to (!=): Checks if two values are not equal.
let ageOfAdam = 25; let ageOfJulia = 24; let isNotEqual = (ageOfAdam != ageOfJulia); console.log(isNotEqual); // trueGreater than (>): Checks if one value is greater than another.
let ageOfAdam = 25; let ageOfJulia = 24; let isGreater = (ageOfAdam > ageOfJulia); console.log(isGreater); // trueGreater 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); // trueLess than (<): Checks if one value is less than another.
let ageOfAdam = 25; let ageOfJulia = 24; let isLess = (ageOfAdam < ageOfJulia); console.log(isLess); // falseLess 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!



