JavaScript operators are symbols or keywords that perform specific actions on one or more values, called operands. They are used to manipulate and combine values, perform arithmetic and logical operations, and compare values. In this article, we will explore the different types of JavaScript operators and provide examples to help you understand their usage.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations on numbers. Here are some commonly used arithmetic operators:
- Addition (+): Adds two values together. For example, 2 + 3 equals 5.
- Subtraction (-): Subtracts one value from another. For example, 5 – 2 equals 3.
- Multiplication (*): Multiplies two values. For example, 2 * 3 equals 6.
- Division (/): Divides one value by another. For example, 6 / 3 equals 2.
- Modulus (%): Returns the remainder of a division operation. For example, 7 % 3 equals 1.
- Exponentiation (**): Raises a value to the power of another value. For example, 2 ** 3 equals 8.
Assignment Operators
Assignment operators are used to assign values to variables. Here are some commonly used assignment operators:
- Assignment (=): Assigns a value to a variable. For example,
var x = 5;
- Addition assignment (+=): Adds a value to the current value of a variable. For example,
x += 3;
is equivalent tox = x + 3;
- Subtraction assignment (-=): Subtracts a value from the current value of a variable. For example,
x -= 2;
is equivalent tox = x - 2;
- Multiplication assignment (*=): Multiplies the current value of a variable by a value. For example,
x *= 2;
is equivalent tox = x * 2;
- Division assignment (/=): Divides the current value of a variable by a value. For example,
x /= 3;
is equivalent tox = x / 3;
Comparison Operators
Comparison operators are used to compare values and return a boolean result (true or false). Here are some commonly used comparison operators:
- Equal to (==): Checks if two values are equal. For example, 5 == 5 returns true.
- Not equal to (!=): Checks if two values are not equal. For example, 5 != 3 returns true.
- Strict equal to (===): Checks if two values are equal and of the same type. For example, 5 === ‘5’ returns false.
- Strict not equal to (!==): Checks if two values are not equal or of different types. For example, 5 !== ‘5’ returns true.
- Greater than (>): Checks if one value is greater than another. For example, 5 > 3 returns true.
- Less than (<): Checks if one value is less than another. For example, 3 < 5 returns true.
- Greater than or equal to (>=): Checks if one value is greater than or equal to another. For example, 5 >= 5 returns true.
- Less than or equal to (<=): Checks if one value is less than or equal to another. For example, 3 <= 5 returns true.
Logical Operators
Logical operators are used to combine or negate boolean values. Here are the three logical operators in JavaScript:
- Logical AND (&&): Returns true if both operands are true. For example, true && false returns false.
- Logical OR (||): Returns true if at least one of the operands is true. For example, true || false returns true.
- Logical NOT (!): Negates the boolean value of an operand. For example, !true returns false.
Unary Operators
Unary operators operate on a single operand. Here are some examples of unary operators:
- Increment (++): Increases the value of a variable by 1. For example,
var x = 5; x++;
increases the value of x to 6. - Decrement (–): Decreases the value of a variable by 1. For example,
var x = 5; x--;
decreases the value of x to 4. - Negation (-): Negates the value of an operand. For example, -5 returns -5.
- Typeof: Returns the type of a value. For example,
typeof 5
returns “number”.
These are just a few examples of JavaScript operators. Understanding how to use operators is essential for writing effective JavaScript code. By using operators correctly, you can perform a wide range of operations and manipulate values in your programs.