Javascript

A Comprehensive Guide to JavaScript Operators

JavaScript operators are essential tools for performing various operations on values and variables. In this guide, we’ll explore the different types of operators in JavaScript and provide code examples to help you understand how each one works.

1. Arithmetic Operators

Arithmetic operators are used for basic mathematical operations:

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /
  • Modulus (Remainder) %
  • Exponentiation ** (ES6)

Example:

let addition = 5 + 3;        // 8
let subtraction = 10 - 4;     // 6
let multiplication = 4 * 6;  // 24
let division = 15 / 3;       // 5
let modulus = 17 % 4;        // 1
let exponentiation = 2 ** 3; // 8

2. Assignment Operators

Assignment operators are used to assign values to variables and update their values:

  • Assignment =
  • Addition assignment +=
  • Subtraction assignment -=
  • Multiplication assignment *=
  • Division assignment /=
  • Modulus assignment %=
  • Exponentiation assignment **= (ES6)

Example:

let x = 5;
x += 3;  // x is now 8
x -= 2;  // x is now 6
x *= 4;  // x is now 24
x /= 6;  // x is now 4
x %= 3;  // x is now 1
x **= 2; // x is now 1 (ES6)

3. Comparison Operators

Comparison operators are used for comparing values:

  • Equality == (type coercion)
  • Strict equality ===
  • Inequality != (type coercion)
  • Strict inequality !==
  • Greater than >
  • Less than <
  • Greater than or equal to >=
  • Less than or equal to <=

Example:

let isEqual = 5 == "5";        // true
let isStrictEqual = 5 === "5";  // false
let isNotEqual = 5 != 8;       // true
let isNotStrictEqual = 5 !== 8; // true
let isGreater = 10 > 7;        // true
let isLess = 3 < 2;            // false
let isGreaterOrEqual = 6 >= 6; // true
let isLessOrEqual = 5 <= 4;    // false

4. Logical Operators

Logical operators are used for combining and manipulating boolean values:

  • Logical AND &&
  • Logical OR ||
  • Logical NOT !

Example:

let logicalAnd = true && false; // false
let logicalOr = true || false;   // true
let logicalNot = !true;          // false

5. Unary Operators

Unary operators operate on a single operand or variable:

  • Increment ++
  • Decrement --
  • Negation (Unary Minus) -
  • Typeof typeof
  • Void void
  • Delete delete

Example:

let number = 5;
number++;       // Increment (6)
number--;       // Decrement (5)
let negate = -number;    // Negation (-5)
let type = typeof number; // Typeof operator

6. Bitwise Operators

Bitwise operators are used for binary-level operations:

  • Bitwise AND &
  • Bitwise OR |
  • Bitwise XOR ^
  • Bitwise NOT ~
  • Left shift <<
  • Right shift >>
  • Unsigned right shift >>>

Example:

let a = 5;       // Binary: 0101
let b = 3;       // Binary: 0011
let bitwiseAnd = a & b;   // 1 (Decimal)
let bitwiseOr = a | b;    // 7 (Decimal)
let bitwiseXor = a ^ b;   // 6 (Decimal)
let bitwiseNot = ~a;      // Negative decimal number
let leftShift = a << 2;   // 20 (Decimal)
let rightShift = a >> 1;  // 2 (Decimal)
let unsignedRightShift = a >>> 1; // 2 (Decimal)

These are the fundamental JavaScript operators that you’ll use in your coding journey. Understanding how each operator works is crucial for writing efficient and error-free code. Experiment with these operators to gain a deeper understanding of their functionality. Happy coding!

Danilo Cavalcante

Working with web development since 2005, currently as a senior programmer analyst. Development, maintenance, and integration of systems in C#, ASP.Net, ASP.Net MVC, .Net Core, Web API, WebService, Integrations (SOAP and REST), Object-Oriented Programming, DDD, SQL, Git, and JavaScript

Recent Posts

Classes and Objects in C#: Object-Oriented Programming

In the world of C# and object-oriented programming (OOP), classes and objects form the backbone…

42 minutes ago

Collections and LINQ Queries in C#

In modern C# programming, working with data collections is a common task. Understanding how to…

2 days ago

Exception Handling in C#: try-catch, finally, and Custom Exceptions

Exception handling is a critical part of writing robust and maintainable C# applications. It allows…

3 days ago

Do Docker Containers Take Up Space?

One of the common questions among Docker users is whether Docker containers consume disk space.…

6 months ago

How to Use “Order By” in C#

Sorting data is a common operation in programming, allowing you to organize information in a…

6 months ago

How to Split a String into an Array in C#

Splitting a string into an array of substrings is a common operation in C# programming,…

6 months ago