Operators in Javascript are special symbols that perform mathematical, logical, and comparison operations on values. They are essential for Javascript programming as they allow developers to perform calculations and make decisions based on the values ​​of variables.

Arithmetic Operators

Arithmetic operators in Javascript are used to perform mathematical operations on numeric values. They include:

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

For example, the code below uses arithmetic operators to calculate the average of three numbers:

let num1 = 10;
let num2 = 5;
let num3 = 8;

let average = (num1 + num2 + num3) / 3;

console.log(media); // Output: 7.67

Assignment Operators

Assignment operators in Javascript are used to assign values ​​to variables. They include:

  • =: Simple Assignment
  • +=: Assignment with addition
  • -=: Assignment with subtraction
  • *=: Assignment with multiplication
  • /=: Assignment with division
  • %=: Assignment with module

For example, the code below uses the plus assignment operator to increment the value of a variable:

let num = 5;

num += 3; // Equivalent to num = num + 3;

console.log(num); // Output: 8

Comparison Operators

Comparison operators in Javascript are used to compare values ​​and return a boolean (true or false) value. They include:

  • ==: Equality
  • !=: Difference
  • ===: Strict equality (type comparison)
  • !==: Strict difference (type comparison)
  • >: Greater Than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

For example, the code below uses the comparison operator to check if one number is greater than another:

let num1 = 5;
let num2 = 3;

if (num1 > num2) {
  console.log("num1 is greater than num2");
} else {
  console.log("num2 is greater than num1");
}

Logical Operators

Logical operators in Javascript are used to combine boolean values ​​and return a boolean value. They include:

  • &&: It is logical
  • ||: Logical Or
  • !: Logical negation

For example, the code below uses the logical AND operator to check whether two conditions are true:

let num1 = 5;
let num2 = 3;

if (num1 > 0 && num2 > 0) {
  console.log("Both numbers are positive");
} else {
  console.log("At least one of the numbers is negative");
}

Ternary Operator

The ternary operator in Javascript is used to do a conditional evaluation in a single line of code. It has the following syntax:

condition ? value_if_true : value_if_false;

For example, the code below uses the ternary operator to check whether a number is even or odd:

let num = 5;

let parity = num % 2 == 0 ? "even" : "odd";

console.log(parity); // Output: odd

In short, operators in Javascript are powerful tools for performing mathematical, logical, and comparison operations on values. By mastering operators in Javascript, developers can write more efficient and expressive code.

Now answer the exercise about the content:

_What is the role of comparison operators in Javascript?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Flow Control Structures in Javascript 5

Next page of the Free Ebook:

Flow Control Structures in Javascript

Estimated reading time: 2 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks