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.

Javascript vs PHP, which to start learning first for beginners?

Javascript has been getting very popular in the past 2 years mainly because together with HTML5, you can create iPhone or Android apps.

Though it is known that in terms of speed, Javascript will never be able to catch up with native apps, there are still many applications that are suitable for developing with Javascript.

If you delve into these 2 languages, you would be surprised how different they are.  You can say C and Java are very similar but you can’t say PHP and Javascript are similar.

What makes PHP so popular? It’s the simple reason that PHP is server side and the code written is hidden in the server.

If you are a new web programmer, I strongly suggest that you start with Javascript first.  Javascript has a better separation between the script and HTML making it a lot more easy to code.

When you first learn about Javascript, you do not have to worry about HTML or CSS.  You can learn it as if you are writing in C.  However, when you are writing PHP, you need to put the HTML into the code making it really confusing for the beginners.