Operators

You need operators when performing any operation in the JavaScript language. Operations include addition, subtraction, comparison, and so on. There are four types of operators in the JavaScript language:

  • Arithmetic

  • Assignment

  • Comparison

  • Logical

Arithmetic operators

Arithmetic operators perform basic mathematic operations such as addition, subtraction, multiplication, division, and so on. Below lists and describes all the arithmetic operators that are available in the JavaScript language.

Arithmetic operators

Assignment operators

While arithmetic operators perform basic mathematic operations, assignment operators assign values to JavaScript variables. You saw the most common assignment operator when you assigned values to variables in the previous section. Below lists and describes all the assignment operators that are available in the JavaScript language.

Assignment operatorsYou already saw how to use the equal sign to assign a value and expression to a variable, but now I'll show you how to use one that can be a little more confusing. Assigning an addition value to a variable can be a strange concept at first, but it's actually pretty simple (Below).

Assigning an addition value to a variable

var num = 10;

document.write("The value of num is :"+num);

// Update the value of num to 15

num += 5;

document.write("The value of num is :"+num);

The result of this script is "The value of num is: 10" followed by "The new value of num is: 15." You can see that the operator in this script assigns the addition value to the variable. This can also be a shorthand way of writing the script shown below.

The longer way to assign an addition value to a variable

var num = 10;

document.write("The value of num is :"+num);

// Update the value of num to 15

num = (num + 5);

document.write("The value of num is :"+num);

Comparison operators

Comparison operators determine the relationship between variables or their values. You use comparison operators inside conditional statements to create logic by comparing variables or their values to evaluate whether a statement is true or false. Below lists and describes all the comparison operators available in the JavaScript language.

Comparison operatorsComparing variables and values is fundamental to writing any sort logic. The example below shows how to use the equal to comparison operator (==) to determine whether 10 is equal to 1.

Using a comparison operator

document.write(10 == 1);

Logical operators

Logical operators are generally used in conditional statements to combine comparison operators. Below lists and describes all the logical operators available in the JavaScript language.

Logical operators