Lesson 4 of 16
In Progress

# Operators and Expressions

In JavaScript, operators are special symbols that perform specific operations on values and variables. These operations can be arithmetic, logical, or assignment in nature.

Expressions are combinations of values, variables, and operators that evaluate to a single value. They are a fundamental part of any programming language and are used to perform calculations, make comparisons, and assign values.

In this article, we’ll go over the different types of operators and expressions in JavaScript and how to use them in your code.

## Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic operations like addition, subtraction, multiplication, and division.

Here are the most common arithmetic operators in JavaScript:

• +: Addition
• -: Subtraction
• *: Multiplication
• /: Division
• %: Modulus (remainder)
• ++: Increment
• --: Decrement

Here’s an example of how to use these operators in an expression:

var result = 3 + 5; // 8
result = result * 2; // 16
result = result / 4; // 4
result = result % 3; // 1


## Comparison Operators

Comparison operators are used to compare values and return a boolean value of true or false.

Here are the most common comparison operators in JavaScript:

• ==: Equal to
• !=: Not equal to
• >: Greater than
• <: Less than
• >=: Greater than or equal to
• <=: Less than or equal to

Here’s an example of how to use comparison operators in an expression:

var result = 3 > 5; // false
result = 3 >= 3; // true
result = "a" == "a"; // true
result = "a" != "b"; // true


## Logical Operators

Logical operators are used to perform logical operations like and, or, and not.

Here are the most common logical operators in JavaScript:

• &&: And
• ||: Or
• !: Not

Here’s an example of how to use logical operators in an expression:

var result = true && true; //
true result = true || false; // true
result = !true; //false

## Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is the equal sign (=), which assigns a value to a variable.

There are also compound assignment operators that perform an operation and assign the result to a variable in a single step. These include:

• +=: Addition assignment
• -=: Subtraction assignment
• *=: Multiplication assignment
• /=: Division assignment

Here’s an example of how to use assignment operators in an expression:

var result = 0;
result += 10; // 10
result -= 5; // 5
result *= 2; // 10
result /= 2; // 5

## Operator Precedence

In JavaScript, some operators have higher precedence than others, which means they are evaluated before other operators. For example, the multiplication operator has a higher precedence than the addition operator, so the following expression:

var result = 2 + 3 * 4;

will evaluate to 14, not 20, because the multiplication is performed before the addition.

You can use parentheses to specify the order in which operations should be performed, like this:

var result = (2 + 3) * 4;

This will evaluate to 20, because the addition is performed first inside the parentheses.

## Conclusion

Operators and expressions are an essential part of JavaScript programming. By understanding how to use different types of operators and how to create and evaluate expressions, you’ll have the skills you need to build powerful and sophisticated web applications with JavaScript.

## Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

#### Use arithmetic operators to calculate the result of the following expression: (10 + 5) * 2 / 5.

var result = (10 + 5) * 2 / 5;
// result = 15


#### Use comparison operators to compare the following values and assign the result to a variable: 5 and "5".

var result = 5 == "5";
// result = true


#### Use logical operators to evaluate the following expression: (true && false) || (true || false) && !false.

var result = (true && false) || (true || false) && !false;
// result = true


#### Use operator precedence to evaluate the following expression: 2 + 3 * 4 - 5 / 2.

var result = 2 + 3 * 4 - 5 / 2;
// result = 14


#### Use parentheses to specify the order in which operations should be performed in the following expression: 2 + 3 * 4 - 5 / 2.

var result = (2 + 3) * 4 - (5 / 2);
// result = 20