JavaScript Operators

JavaScript operators are symbols that perform operations on one or more operands. Operands can be variables, values, or expressions. Operators are used to perform a variety of tasks, such as arithmetic operations, logical comparisons, and string manipulation.

Types of JavaScript Operators

There are seven main types of JavaScript operators:

Arithmetic operators: Arithmetic operators perform mathematical operations on numbers. For example, the + operator adds two numbers together, the - operator subtracts two numbers, and the * operator multiplies two numbers.

Assignment operators: Assignment operators assign values to variables. For example, the = operator assigns a value to a variable, and the += operator adds a value to a variable that already has a value.

Comparison operators: Comparison operators compare two values and return a Boolean value (true or false). For example, the == operator checks if two values are equal, and the > operator checks if one value is greater than another value.

Logical operators: Logical operators combine Boolean values to produce a new Boolean value. For example, the && operator returns true if both operands are true, and the || operator returns true if either operand is true.

String operators: String operators perform operations on strings. For example, the + operator concatenates two strings together, and the [] operator returns a character at a specific index in a string.

Bitwise operators: Bitwise operators perform operations on bits. For example, the & operator performs a bitwise AND operation, and the | operator performs a bitwise OR operation.

Ternary operators: The ternary operator is a special operator that can be used to evaluate three expressions and return one of two values depending on the result of the first expression.

Examples of JavaScript Operators

Here are some examples of how to use JavaScript operators:

// Arithmetic operators
const sum = 1 + 2; // 3
const difference = 5 - 2; // 3
const product = 3 * 4; // 12
const quotient = 10 / 2; // 5

// Assignment operators
let number = 10;
number += 5; // number now equals 15
number *= 2; // number now equals 30

// Comparison operators
const isEqualTo = 10 === 10; // true
const isGreaterThan = 10 > 5; // true
const isLessThanOrEqualTo = 5 <= 5; // true

// Logical operators
const isTrue = true && false; // false
const isFalse = true || false; // true

// String operators
const greeting = "Hello" + " world!"; // “Hello world!”
const firstCharacter = greeting[0]; // “H”

// Bitwise operators
const bitwiseAnd = 10 & 5; // 0
const bitwiseOr = 10 | 5; // 15

// Ternary operator
const isAdult = age >= 18 ? true : false;

Operator Precedence

Operator precedence is the order in which operators are evaluated. Operators with higher precedence are evaluated first. If two operators have the same precedence, the operator from left to right is evaluated first.

You can use parentheses to change the order of evaluation. For example, the expression (10 + 5) * 2 will be evaluated as follows:

  1. The expression 10 + 5 is evaluated first, returning the value 15.
  2. The expression 15 * 2 is then evaluated, returning the value 30.

Without parentheses, the expression would be evaluated as follows:

  1. The operator * has higher precedence than the operator +, so the expression 10 * 2 is evaluated first, returning the value 20.
  2. The expression 20 + 5 is then evaluated, returning the value 25.

Therefore, it is important to understand operator precedence when writing JavaScript code.

Conclusion

JavaScript operators are a powerful tool that can be used to perform a variety of tasks. By understanding the different types of operators and their precedence, you can write more efficient and readable code.