Operator

In programming, an operator is a symbol or a function that performs an operation on one or more operands to produce a result. It is used to manipulate data values in expressions or statements. Operators are used in various programming languages, such as JavaScript, Python, and C++.

Here are some common types of operators:

  1. Arithmetic Operators: Arithmetic operators are used to perform arithmetic operations on numeric data types. Examples include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

  2. Comparison Operators: Comparison operators are used to compare two values and produce a Boolean result (true or false). Examples include equal to (==), not equal to (!=), greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=).

  3. Logical Operators: Logical operators are used to combine Boolean values and produce a Boolean result. Examples include AND (&&), OR (||), and NOT (!).

  4. Assignment Operators: Assignment operators are used to assign values to variables. Examples include equals (=), addition and assignment (+=), subtraction and assignment (-=), multiplication and assignment (*=), division and assignment (/=), and modulus and assignment (%=).

  5. Bitwise Operators: Bitwise operators are used to perform operations on binary numbers. Examples include bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>).

Using operators in programming can help manipulate data values and perform calculations or comparisons. It is important to understand the behavior and precedence of different types of operators to ensure correct results in your program.

  1. Arithmetic operators:

let x = 5;
let y = 2;
console.log(x + y); // Output: 7
console.log(x - y); // Output: 3
console.log(x * y); // Output: 10
console.log(x / y); // Output: 2.5
console.log(x % y); // Output: 1
  1. Comparison operators:

let x = 5;
let y = 2;
console.log(x > y); // Output: true
console.log(x < y); // Output: false
console.log(x >= y); // Output: true
console.log(x <= y); // Output: false
console.log(x === y); // Output: false
console.log(x !== y); // Output: true
  1. Logical operators:

let x = 5;
let y = 2;
console.log(x > 3 && y < 4); // Output: true
console.log(x < 3 || y > 4); // Output: false
console.log(!(x > y)); // Output: false
  1. Assignment operators:

let x = 5;
x += 2;
console.log(x); // Output: 7
x -= 2;
console.log(x); // Output: 5
x *= 2;
console.log(x); // Output: 10
x /= 2;
console.log(x); // Output: 5
x %= 2;
console.log(x); // Output: 1
  1. Bitwise operators:

let x = 5;
let y = 2;
console.log(x & y); // Output: 0
console.log(x | y); // Output: 7
console.log(x ^ y); // Output: 7
console.log(~x); // Output: -6
console.log(x << 1); // Output: 10
console.log(x >> 1); // Output: 2

Last updated