Javascript: Operators

This entry is part 14 of 27 in the series Javascript Tutorial

There are a few types of Javascript operators.

  • Assignment
  • Comparison
  • Arithmetic
  • Bitwise
  • Logical
  • String

The commonly used operators are assignment, comparison, arithmetic and string operators.

The standard arithmetic operators are addition (+), subtraction (), multiplication (*), and division (/).

In an earlier example, we have made use of + operator which is an arithmetic operator.

Comparison Operators

OperatorDescription
Equal (==)Returns true if the operands are equal.
Not equal (!=)Returns true if the operands are not equal.
Greater than (>)Returns true if the left operand is greater than the right operand.
Greater than or equal (>=)Returns true if the left operand is greater than or equal to the right operand.
Less than (<)Returns true if the left operand is less than the right operand.
Less than or equal (<=)Returns true if the left operand is less than or equal to the right operand.

String Operators

The string operator can be used on string values.

The concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings.

For example, “my ” + “string” returns the string “my string”.

<!DOCTYPE html>
<html>
<head>
 <title>String Operator +</title>
 <script>
   function strFunction() {
    var x = "String Theory";
    var y = "Simplified";
    var z = x + " " + y;
    document.getElementById("str1").innerHTML = x;
    document.getElementById("str2").innerHTML = y;
    document.getElementById("str3").innerHTML = z;
   }
 </script>
</head>
<body>
 <p id="str1">String 1</p>
 <p id="str2">String 2</p>
 <p id="str3">String 1 + String 2</p>
 <br>
 <button onclick="strFunction()">Click Me</button>
 </body>
</html>

button

Notice that in the result string, we have used

var z = x + " " + y;

Adding ” “ will add a space in between the 2 strings.

Series Navigation<< Javascript: Functions
Javascript: If Else >>