Understanding Operators
Operators are symbols or keywords used to perform operations on one or more operands (values, variables, or expressions) to produce a result. They are a fundamental aspect of programming languages and play a crucial role in manipulating data, making decisions, and performing various computations. Operators define the behavior of a programming language by specifying how different types of values interact with each other. Here’s an explanation of operators:
# | Name | Description | Example |
---|---|---|---|
1 | Arithmetic Operators | Perform basic arithmetic operations like addition, subtraction, multiplication, division, modulo, and exponentiation. | x = 5 + 3; // x becomes 8 |
2 | Comparison Operators | Compare values and return Boolean results based on the comparison. | result = (a > b); // result is true or false |
3 | Logical Operators | Combine or manipulate Boolean values in conditional statements. | if (condition1 && condition2) { /* code */ } |
4 | Assignment Operators | Assign values to variables and perform arithmetic operations while assigning. | y += 10; // y becomes y + 10 |
5 | Increment and Decrement Operators | Increase or decrease variable values by a certain amount. | count++; // increment count by 1 |
6 | Bitwise Operators | Perform operations at the bit level, useful for binary data manipulation. | x = a & b; // bitwise AND |
7 | Conditional Operator | Provide a shorthand way to write conditional statements. | result = (condition) ? value_if_true : value_if_false; |
8 | Type Operators | Determine the type of a value or check inheritance relationships in OOP. | typeof x; // returns the type of x |
9 | String Concatenation Operator | Combine strings together. | message = “Hello, ” + name; |
10 | Special Operators | Additional operators for specific tasks, like array indexing or pointer manipulation. | pointer = &variable; // pointer to variable |
JavaScript Operators
Arithmetic Operators
Arithmetic operators in programming are symbols used to perform mathematical operations on numeric values. These operations include addition, subtraction, multiplication, division, and more. They allow developers to manipulate numbers and perform calculations within their code. Here’s a brief overview of common arithmetic operators:
# | Name | Symbol | Description | Example |
---|---|---|---|---|
1 | Addition | + | Adds two operands | x = 5 + 3; |
2 | Subtraction | – | Subtracts second operand from first | x = 10 – 5; |
3 | Multiplication | * | Multiplies two operands | x = 4 * 2; |
4 | Division | / | Divides first operand by second | x = 10 / 2; |
5 | Modulo (Remainder) | % | Returns remainder of division | x = 10 % 3; |
6 | Exponentiation | ** | Raises first operand to power of second | x = 2 ** 3; |
Assignment Operators
Assignment operators in programming are used to assign values to variables. They allow you to store data or results of computations in variables for later use. These operators simplify the process of updating variables with new values. Here’s a brief overview of common assignment operators:
# | Name | Symbol | Description | Example |
---|---|---|---|---|
1 | Assignment | = | Assigns value to a variable | x = 5; |
2 | Add and Assign | += | Adds value and assigns | x += 3; |
3 | Subtract and Assign | -= | Subtracts value and assigns | x -= 2; |
4 | Multiply and Assign | *= | Multiplies value and assigns | x *= 4; |
5 | Divide and Assign | /= | Divides value and assigns | x /= 2; |
6 | Modulo and Assign | %= | Performs modulo and assigns | x %= 3; |
7 | Exponentiation and Assign | **= | Exponentiates and assigns | x **= 2; |
Comparison Operators
Comparison operators in programming are used to compare two values and determine the relationship between them. They help in making decisions and controlling the flow of a program based on conditions. Here’s a brief overview of common comparison operators:
# | Name | Symbol | Description | Example |
---|---|---|---|---|
1 | Equal to | == | Checks if operands are equal | a == b; |
2 | Not Equal to | != | Checks if operands are not equal | a != b; |
3 | Equal to (Strict) | === | Checks if operands are equal and of same type | a === b; |
4 | Not Equal to (Strict) | !== | Checks if operands are not equal or different type | a !== b; |
5 | Greater Than | > | Checks if first operand is greater | a > b; |
6 | Less Than | < | Checks if first operand is less | a < b; |
7 | Greater Than or Equal to | >= | Checks if first operand is greater or equal | a >= b; |
8 | Less Than or Equal to | <= | Checks if first operand is less or equal | a <= b; |
Logical Operators
Logical operators in programming are used to combine or manipulate Boolean values, which represent true or false conditions. These operators are used to create complex logic and control the flow of a program based on multiple conditions. Here’s a brief overview of common logical operators:
# | Name | Symbol | Description | Example |
---|---|---|---|---|
1 | AND | && | Returns true if both operands are true | a && b; |
2 | OR | || | Returns true if at least one operand is true | a || b; |
3 | NOT | ! | Returns true if operand is false, and vice versa | !a; |
Unary Operators
a unary operation or unary operator is an operation that involves only one operand, or input value. This type of operation typically takes a single value and produces a result based on that value and the operator being used.
The term “unary” comes from the Latin word “unus,” which means “one.” Unary operators are used to perform operations on a single value, and they are an essential part of programming languages for manipulating data and making computations.
# | Name | Symbol | Description | Example |
---|---|---|---|---|
1 | Unary Plus (+) | + | Converts its operand into a number or maintains its sign. | +a; |
2 | Unary Minus (-) | – | Negates the sign of its operand. | -a; |
3 | Increment (++) | ++ | Increases the value of its operand by 1. | a++; |
4 | Decrement (–) | — | Decreases the value of its operand by 1. | a–; |
5 | Logical NOT (!) | ! | Negates the truthiness of its operand (converts true to false and vice versa). | !a; |
6 | Bitwise NOT (~) | ~ | Inverts the bits of its operand. | ~a; |
7 | Typeof (typeof) | typeof | Returns a string indicating the data type of its operand. | typeof a; |
Bitwise Operators
Bitwise operators in programming are used to manipulate individual bits of integer values at a binary level. These operators perform operations on the binary representation of the numbers, allowing for bitwise calculations. Here’s a brief overview of common bitwise operators:
# | Name | Symbol | Description | Example |
---|---|---|---|---|
1 | AND | & | Performs bitwise AND operation | a & b; |
2 | OR | | | Performs bitwise OR operation | a | b; |
3 | XOR | ^ | Performs bitwise XOR operation | a ^ b; |
4 | NOT | ~ | Performs bitwise NOT operation | ~a; |
5 | Left Shift | << | Shifts bits to the left | a << 2; |
6 | Right Shift | >> | Shifts bits to the right | a >> 1; |
7 | Zero-fill Right Shift | >>> | Shifts bits to the right with zero fill | a >>> 3; |
Additional JavaScript Operators
Conditional (Ternary) Operator
condition ? value_if_true : value_if_false
Type Operators
typeof (Determine Type)
instanceof (Check Instance)
String Operators
+ (Concatenation)
Increment and Decrement Operators
++ (Increment)
— (Decrement)
Comma Operator
, (Comma)
Member Access Operators
. (Dot notation)
[] (Bracket notation)
Please note that this is not an exhaustive list of all operators, but it covers the most commonly used ones in JavaScript. Each operator has specific behavior and usage, and understanding them is crucial for writing effective JavaScript code.
Examples of Operators in JavaScript
// Arithmetic Operators let x = 10; let y = 5; let addition = x + y; let subtraction = x - y; let multiplication = x * y; let division = x / y; let modulo = x % y; let exponentiation = x ** y; // Comparison Operators let a = 15; let b = 20; console.log(a > b); console.log(a = b); console.log(a = 18) ? "Adult" : "Minor"; console.log(status);