JavaScript Operators
JavaScript operators are special symbols that perform operations on variables and values. Here are some key types of operators in JavaScript:
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
- + (Addition): Adds two values
- - (Subtraction): Subtracts one value from another
- * (Multiplication): Multiplies two values
- / (Division): Divides one value by another
- % (Modulus): Returns the remainder of a division
- ** (Exponentiation): Raises the first value to the power of the second
2. Comparison Operators
Comparison operators are used to compare two values and return a Boolean result.
- == (Equal to): Checks if two values are equal
- === (Strict equal to): Checks if two values are equal and of the same type
- != (Not equal to): Checks if two values are not equal
- !== (Strict not equal to): Checks if two values are not equal or not of the same type
- > (Greater than): Checks if one value is greater than another
- << /strong> (Less than): Checks if one value is less than another
- >= (Greater than or equal to): Checks if one value is greater than or equal to another
- <=< /strong> (Less than or equal to): Checks if one value is less than or equal to another
3. Logical Operators
Logical operators are used to combine Boolean values.
- && (Logical AND): Returns true if both operands are true
- || (Logical OR): Returns true if at least one operand is true
- ! (Logical NOT): Returns true if the operand is false
4. Assignment Operators
Assignment operators are used to assign values to variables.
- = (Assignment): Assigns a value to a variable
- += (Addition assignment): Adds a value to a variable
- -= (Subtraction assignment): Subtracts a value from a variable
- *= (Multiplication assignment): Multiplies a variable by a value
- /= (Division assignment): Divides a variable by a value
- %= (Modulus assignment): Applies modulus and assigns the result
5. Unary Operators
Unary operators operate on a single operand.
- ++ (Increment): Increases a value by one
- -- (Decrement): Decreases a value by one
- typeof (Typeof): Returns the type of a variable
- void (Void): Evaluates an expression and returns undefined
- ! (Logical NOT): Inverts a Boolean value
6. Ternary Operator
The ternary operator is a shorthand for the if-else statement.
condition ? expr1 : expr2 - If the condition is true, expr1 is executed; otherwise, expr2 is executed.
7. typeof Operator
The typeof operator returns a string indicating the type of the unevaluated operand.
typeof "Hello"
returns "string"
8. In Operator
The in operator checks if a property exists in an object.
"name" in person
returns true
if name
is a property of the person
object.
9. Instanceof Operator
The instanceof operator tests if an object is an instance of a constructor.
obj instanceof Constructor
returns true
if obj
is an instance of Constructor
.
10. Delete Operator
The delete operator removes a property from an object.
delete obj.property
returns true
if the property was successfully deleted.