High-level programming language.
Operators are the building blocks of any programming language. They allow us to perform operations on our data. In JavaScript, there are several types of operators: arithmetic, assignment, comparison, logical, and the ternary operator. This article will provide a comprehensive overview of each.
Arithmetic operators are used to perform mathematical operations:
+
): Adds two numbers together.-
): Subtracts one number from another.*
): Multiplies two numbers together./
): Divides one number by another.%
): Returns the remainder of a division operation.++
): Increases a number by 1.--
): Decreases a number by 1.Assignment operators are used to assign values to variables:
=
): Assigns a value to a variable.+=
): Adds a value to a variable and assigns the result to that variable.-=
): Subtracts a value from a variable and assigns the result to that variable.*=
), division (/=
), and modulus (%=
).Comparison operators are used to compare two values:
==
): Checks if two values are equal.!=
): Checks if two values are not equal.>
): Checks if one value is greater than another.<
): Checks if one value is less than another.>=
): Checks if one value is greater than or equal to another.<=
): Checks if one value is less than or equal to another.===
): Checks if two values are equal in value and type.!==
): Checks if two values are not equal in value or type.Logical operators are used to determine the logic between variables or values:
&&
): Returns true if both operands are true.||
): Returns true if either operand is true.!
): Returns true if the operand is false, and false if the operand is true.The ternary operator is a shorthand way of writing an if...else
statement. It takes three operands: a condition, a value to return if the condition is true, and a value to return if the condition is false.
condition ? valueIfTrue : valueIfFalse
Operator precedence determines the order in which operations are performed when there are different operators in an expression. For example, multiplication and division have higher precedence than addition and subtraction.
Associativity determines the order in which operations are performed when there are two or more operators of the same precedence in an expression. For example, assignment operators are right-associative, meaning they are evaluated from right to left.
Understanding these operators is crucial to writing effective JavaScript code. They allow us to manipulate our data and control the flow of our programs.