JavaScript Operators

JavaScript addition operator

 
The JavaScript addition operator ( + ) serves two main purposes in JavaScript.
 
The first is to perform a simple addition on numbers.
 
The second operation is to perform string concatenation (combine strings).
 
1. Addition
  1. <script>  
  2.     var a = 3;  
  3.     var b = 2;  
  4.     var c = a + b;  
  5.     document.write(c);  
  6. </script> 
addition-operator-in-javascript.png
 
2. String Concatenation
  1. <script>  
  2.     var user = "Henry";  
  3.     var country = "USA";  
  4.     var output = user + " is from " + country;  
  5.     document.write(output);  
  6. </script> 
string-operator-in-javascript.png
 

JavaScript subtraction operator

 
The JavaScript subtraction operator ( - ) also serves two purposes in your code.
 
The first is to perform simple subtraction on numbers (6 - 2).
 
The second operation is to specify negative numeric values (-20).
 
1. Subtraction
  1. <script>  
  2.     var a = 10;  
  3.     var b = 3;  
  4.     var c = a - b;  
  5.     document.write(c);  
  6. </script> 
subtraction-operator-in-javascript.png
 
2. Assigning negative values
  1. <script>  
  2.     var a = 10;  
  3.     var b = -10;  
  4.     var c = a + b;  
  5.     document.write(c);  
  6. </script> 
negative-operator-in-javascript.png
 
JavaScript multiplication operator
 
The JavaScript multiplication operator ( * ) is used to multiply numbers.
  1. <script>  
  2.     var a = 4;  
  3.     var b = 3;  
  4.     var c = a * b;  
  5.     document.write(c);  
  6. </script> 
multiply-operator-in-javascript.png
 

JavaScript division operator

 
The JavaScript division operator ( / ) is used to divide numbers.
  1. <script>  
  2.     var a = 8;  
  3.     var b = 4;  
  4.     var c = a / b;  
  5.     document.write(c);  
  6. </script> 
division-operator-in-javascript.png
 

JavaScript increment operator

 
The JavaScript increment operator ( ++ ) is used to increase a number by 1. 
 
Example 1
  1. <script>  
  2.     var i = 0;  
  3.     i++;  
  4.     document.write(i);  
  5. </script> 
increment-operator-1-in-javascript.png
 
Example 2
  1. <script>  
  2.     for (var i = 0; i < 5; i++)  
  3.     {  
  4.       document.write(i + "<br />");  
  5.     }  
  6. </script> 
increment-operator-2-in-javascript.png
 

JavaScript decrement operator

 
The JavaScript decrement operator ( -- ) is used to decrease a number by 1.
 
Example 1
  1. <script>  
  2.     var i = 1;  
  3.     i--;  
  4.     document.write(i);  
  5. </script> 
decrement-operator-1-in-javascript.png
 
Example 2
  1. <script>  
  2.     for (var i = 10; i > 0; i--)  
  3.     {  
  4.       document.write(i + "<br />");  
  5.     }  
  6. </script> 
decrement-operator-2-in-javascript.png
 

JavaScript modulus operator

 
The JavaScript modulus operator ( % ) is used to calculate the remainder of two values if they were divided.
 
Modulus produces the remainder value only. It is very useful for seeing if values are divisible by a specified number, check Example 2 below to see it in action that way.
 
Example 1
  1. <script>  
  2.     var a = 10;  
  3.     var b = 3;  
  4.     var c = a % b;  
  5.     document.write(c);  
  6. </script> 
modulus-operator-1-in-javascript.png
 
We get the value of "1" as the modulus when 10 is evaluated against 3 because the remainder of dividing those numbers would be "1".
 
Evaluating 10 against 3 gives us an answer of "3 with the remainder of 1".
 
If you have "10" divided by "5" you get a modulus of "0" since there would be no remainder, 5 goes into 10 twice with no remainder left over so we would get modulus of "0".
 
Example 2
  1. <script>  
  2.     for (var i = 1; i <= 20; i++)  
  3.     {  
  4.         if (i % 2 == 0)  
  5.         { // if value is divisible by the number 2 make it red  
  6.          document.write("<span style='color:red;'>" + i + "</span><br />");  
  7.         }  
  8.         else  
  9.         {  
  10.          document.write(i + "<br />");  
  11.         }  
  12.     }  
  13. </script> 
modulus-operator-2-in-javascript.png
 

JavaScript equal operator

 
The JavaScript equal comparison operator ( == ) is used to compare whether the operand on the left is equal to the operand on the right.
 
The expression created will then return a value of either "true" or "false" to you.
 
left_operand == right_operand
  1. <script>  
  2.     var a = 4;  
  3.     var b = 3;  
  4.     document.write(a == b);  
  5. </script> 
equal-operator-1-in-javascript.png
 
You will see it in use most commonly within if...else condition statements.
  1. <script>  
  2.     var a = 4;  
  3.     var b = 3;  
  4.     if (a == b)  
  5.     {  
  6.        document.write("YES THAT IS TRUE");  
  7.     }  
  8.     else  
  9.     {  
  10.        document.write("NO THAT IS FALSE");  
  11.     }  
  12. </script> 
equal-operator-2-in-javascript.png
 

JavaScript strict equal operator

 
The JavaScript strict equal comparison operator ( === ) is similar to the normal equal operator, except that it evaluates the object types also.
 
If the values are the same but the object types are not the same, this will return false whereas the normal equal operator will return true.
 
left_operand === right_operand
  1. <script>  
  2.     var v1 = "5";  
  3.     var v2 = 5;  
  4.     if (v1 === v2)  
  5.     {  
  6.       document.write("TRUE");  
  7.     }  
  8.     else  
  9.     {  
  10.       document.write("FALSE");  
  11.     }  
  12. </script> 
strict-equal-operator-1-in-javascript.png
 
Now run the same exact code using the normal equal operator.
 
You will notice that it does not take object type into consideration at all and will return a value of true even though a left operand is a string object and a right operand is a number object.
  1. <script>  
  2.     var v1 = "5";  
  3.     var v2 = 5;  
  4.     if (v1 == v2)  
  5.     {  
  6.       document.write("TRUE");  
  7.     }  
  8.     else  
  9.     {  
  10.       document.write("FALSE");  
  11.     }  
  12. </script> 
strict-equal-operator-2-in-javascript.png
 

JavaScript not equal operator

 
The JavaScript not equal comparison operator ( != ) is used to check if the operand on the left is not equal to the operand on the right.
 
The expression created will then return a value of either "true" or "false".
 
left_operand != right_operand
  1. <script>  
  2.     var v1 = 30;  
  3.     var v2 = 50;  
  4.     document.write(v1 != v2);  
  5. </script> 
not-equal-operator-in-javascript.png
 

JavaScript strict not equal operator

 
The JavaScript strict not equal comparison operator ( !== ) is used to check if the operand on the left is not equal to the operand on the right, as well as evaluating their object types in the comparison.
 
The expression created will then return a value of either "true" or "false" to you.
 
left_operand !== right_operand
  1. <script>  
  2.     var v1 = "50";  
  3.     var v2 = 50;  
  4.     document.write(v1 !== v2);  
  5. </script> 
strict-not-equal-operator-1-in-javascript.png
 
"50" does not strictly equal 50 because the first is a string object and the second is a number object.
 
Even though the values match, the object types do not.
 
Now run the same exact code using the normal "not equal" operator to see how it does not take object type into consideration in the comparison.
  1. <script>  
  2.     var v1 = "50";  
  3.     var v2 = 50;  
  4.     document.write(v1 != v2);  
  5. </script> 
strict-not-equal-operator-2-in-javascript.png
 
JavaScript less than the operator
 
The JavaScript less than comparison operator ( < ) is used to see if the operand on the left is less than the operand on the right.
 
The expression created will then return a value of either "true" or "false" to you.
 
left_operand < right_operand
  1. <script>  
  2.     var v1 = 4;  
  3.     var v2 = 7;  
  4.     document.write(v1 < v2);  
  5. </script> 
or
  1. <script>  
  2.     var v1 = "abc";  
  3.     var v2 = "def";  
  4.     document.write(v1 < v2);  
  5. </script> 
That will work on strings as well.
 
less-than-operator-in-javascript.png
 

JavaScript greater than operator

 
The JavaScript greater than the comparison operator ( > ) is used to see if the operand on the left is greater than the operand on the right.
 
The expression created will then return a value of either "true" or "false" to you.
 
left_operand > right_operand
  1. <script>  
  2.     var v1 = 4;  
  3.     var v2 = 7;  
  4.     document.write(v1 > v2);  
  5. </script> 
or
  1. <script>  
  2.     var v1 = "abc";  
  3.     var v2 = "def";  
  4.     document.write(v1 > v2);  
  5. </script> 
I Will work on strings as well.
 
greater-than-operator-in-javascript.png
 

JavaScript less than or equal operator

 
The JavaScript less than or equal to comparison operator ( <= ) is used to evaluate if the operand on the left is less than or equal to the operand on the right.
 
The expression created will then return a value of either "true" or "false" to you.
 
left_operand <= right_operand
  1. <script>  
  2.     var v1 = 5;  
  3.     var v2 = 5;  
  4.     document.write(v1 <= v2);  
  5. </script> 
or
  1. <script>  
  2.     var v3 = "c";  
  3.     var v4 = "d";  
  4.     document.write(v3 <= v4);  
  5. </script> 
I Will work on strings as well.
 
less-than-or-equal-operator-in-javascript.png
 

JavaScript greater than or equal operator

 
The JavaScript greater than or equal to comparison operator ( >= ) is used to evaluate if the operand on the left is greater than or equal to the operand on the right.
 
The expression created will then return a value of either "true" or "false" to you.
 
left_operand >= right_operand
  1. <script>  
  2.     var v1 = 100;  
  3.     var v2 = 50;  
  4.     document.write(v1 >= v2);  
  5. </script> 
or
  1. <script>  
  2.     var v3 = "ghi";  
  3.     var v4 = "def";  
  4.     document.write(v3 >= v4);  
  5. </script> 
I Will work on strings as well.
 
greater-than-or-equal-operator-in-javascript.png
 

JavaScript ternary operator

 
The JavaScript ternary operator ( ? ) is sort of like a conditional statement.
 
A ternary operation is one that requires three parameters. It will return the value on the left of the colon ( : ) if the expression is true, and return the value on the right of the colon if the expression is false.
 
(expression) ? "output for true" : "output for false"
  1. <script>  
  2.     var a = 5;  
  3.     var b = 3;  
  4.     var result = (a > b) ? "that is true" : "that is false";  
  5.     document.write(result);  
  6. </script> 
ternary-operator-1-in-javascript.png
 
Here is a more advanced example that will show you how to fire off functions in place of returning simple strings.
  1. <script>  
  2.     var a = 5;  
  3.     var b = 3;  
  4.     function funcForTrue()  
  5.     {  
  6.       document.write("Executed function for a TRUE result");  
  7.     }  
  8.     function funcForFalse()  
  9.     {  
  10.       document.write("Executed function for a FALSE result");  
  11.     }  
  12.     var result = (a > b) ? funcForTrue() : funcForFalse();  
  13. </script> 
ternary-operator-2-in-javascript.png
 

JavaScript append operator

 
The JavaScript append string operator ( += ) serves two separate purposes in our scripts.
  1. First, we use it to append to or compound variable data. 
  2. Secondly, we use it is as a mathematical shorthand to make the left operand equal to the value of both the right and left operands of the expression.

left_operand += right_operand

 
Append to a string or variable
  1. <script>  
  2.     var myVar = "<h2>Welcome to my how-to page</h2>";  
  3.     myVar += "<h3>I Will Explain How To Do This Thing</h3>";  
  4.     myVar += "<p>First, grab yourself a new clean ...<p>";  
  5.     myVar += "<p>Second, be sure to insert it firmly into ...<p>";  
  6.     document.write(myVar);  
  7. </script> 
append-operator-1-in-javascript.png
 
Simple shorthand arithmetic example
  1. <script>  
  2.     var a = 2;  
  3.     var b = 3;  
  4.     document.write(a += b);  
  5. </script> 
append-operator-2-in-javascript.png
 

JavaScript assignment operator

 
The JavaScript assignment operator ( = ) is used to assign the value of the right operand as the new value of the left operand.
 
It is probably the most commonly used operator of them all. Each time you assign values in this way, the existing value that the left operand contained is overwritten with a new value.
 
To append to a variable use the string append operator instead:
 
left_operand = right_operand
  1. <script>  
  2.     var myVar = "Welcome to the circus!";  
  3.     document.write(myVar);  
  4.     document.write("<hr />");  
  5.     myVar = "Welcome to the zoo!";  
  6.     document.write(myVar);  
  7. </script> 
assignment-operator-in-javascript.png
 

JavaScript logical or operator

 
The JavaScript logical or ( || ) operator is used to group values or sub-expressions into one evaluation expression.
 
It makes perfect sense if you replace the characters "||" with the word "or" when you read a script that has this in place.
 
It could help save you a few lines of code here and thereby allowing you to group expressions or values.
 
(value || value)
  1. <script>  
  2.     if ((8 || 35 || 14) < 10)  
  3.     { // if 8 or 35 or 14 is less than 10  
  4.       document.write("At least one value is less than 10");  
  5.    }  
  6.    else  
  7.     {  
  8.       document.write("No values are less than 10");  
  9.     }  
  10. </script> 
logical-or-operator-1-in-javascript.png
 
Here is an example evaluating some string variable data lengths as a group:
  1. <script>  
  2.     var u1 = "John";  
  3.     var u2 = "Sara";  
  4.     var u3 = "Kate";  
  5.     if ((u1.length || u2.length || u3.length) == 4)  
  6.     {  
  7.       document.write("All names have exactly 4 letters in them.");  
  8.     }  
  9.   else  
  10.     {  
  11.       document.write("At least one name does not have 4 letters.");  
  12.     }  
  13. </script> 
logical-or-operator-2-in-javascript.png
 
Here is an example that shows sub-expressions that are grouped together as one expression.
 
Each sub-expression can be wrapped in parenthesis like I did below but that is not a requirement unless your expression logic demands parenthesis grouping.
  1. <script>  
  2.     var sky = "blue";  
  3.     var blood = "green";  
  4.     var sun = "yellow";  
  5.     if ((sky != "blue") || (blood != "red") || (sun != "yellow"))  
  6.     {  
  7.       document.write("Uh Oh! At least one thing is wrong in the world!");  
  8.     }  
  9.   else  
  10.     {  
  11.       document.write("Everything is normal in the world.");  
  12.     }  
  13. </script> 
logical-or-operator-3-in-javascript.png
 

JavaScript logical and operator

 
The JavaScript logical and ( && ) operator is used to group values or sub-expressions where the left and right operands must both evaluate to the same result.
 
It makes good sense if you replace the characters ( && ) with the word "and" when you see that symbol sequence in scripts to get a mental grasp of it.
 
(value && value && value && etc...)
  1. <script>  
  2.     if ((3 && 5 && 12) < 10)  
  3.     {  
  4.       document.write("All values are less than 10");  
  5.     }  
  6.   else  
  7.     {  
  8.       document.write("Not all of the values are less than 10");  
  9.     }  
  10. </script> 
logical-and-operator-1-in-javascript.png
 
Here is an example of evaluating string data.
 
This script is ready to perform a check to make sure all values are present before the program can continue.
  1. <script>  
  2.     var uname = "Josh";  
  3.     var email = "[email protected]";  
  4.     var country = "";  
  5.     if ((uname.length && email.length && country.length))  
  6.     {  
  7.       document.write("We can now proceed because all values are present");  
  8.     }  
  9.   else  
  10.     {  
  11.       document.write("We seem to be missing a value we need, try again");  
  12.     }  
  13. </script> 
logical-and-operator-2-in-javascript.png
 
JavaScript logical, not operator
 
The JavaScript logical not ( ! ) operator is used to evaluate a single operand or expression.
 
It will return true if the operand is empty, and returns false if the operand has some value in it.
 
( !value )
  1. <script>  
  2.     var uname = "";  
  3.     if (!uname)  
  4.     {  
  5.       document.write("Username has no value.");  
  6.     }  
  7.   else  
  8.     {  
  9.       document.write("Username is: " + uname);  
  10.     }  
  11. </script> 
logical-not-operator-in-javascript.png
 

JavaScript comma operator

 
The JavaScript comma ( ! ) operator can be used to apply multiple parameters or arguments to a loop statement.
 
It can be used to establish and iterate multiple variables in a loop, as well as establishing multiple arguments in functions and arrays.
 
( param, param )
  1. <script>  
  2.     var myArray = ["Joe""Paul""Katherine""Susan"];  
  3.     for (var i = 0, x = 3; i < 2; i++, x--)  
  4.     {  
  5.       document.write(myArray[i] + " loves " + myArray[x] + "<br />");  
  6.     }  
  7. </script> 
comma-operator-in-javascript.png
 
JavaScript this operator
 
The JavaScript this special operator is used to dynamically access all of an object's properties.
 
If passed through an HTML element, the element passing it becomes the dynamic object of focus in any functions or methods that the element is invoking in JavaScript.
  1. <a onclick="alert(this.innerHTML)" href="#" onmousedown="return false">  
  2.     If you click me I will kick you in the teeth  
  3. </a> 
this-operator-1-in-javascript.png
 
This example really shows how to gain access to all properties of an element dynamically.
  1. <script type="text/javascript">  
  2.     function learnAbout(x)  
  3. {  
  4.     var result = "Element ID --- "+x.id+"<br />";  
  5.     result += "Element Content --- "+x.innerHTML+"<br />";  
  6.     result += "Element Title --- "+x.title+"<br />";  
  7.     result += "Element --- "+x;  
  8.     document.getElementById("status").innerHTML = result;  
  9. }  
  10. </script>  
  11. <button onclick="learnAbout(this)" id="myBtn" title="cricker">  
  12.   please click me  
  13. </button>  
  14. <a onclick="learnAbout(this)" href="#" id="myLink" title="linker" onmousedown="return false">  
  15.   click me too please  
  16. </a>  
  17. <p id="status"></p> 
this-operator-2-in-javascript.png
 
this-operator-3-in-javascript.png
 
this-operator-4-in-javascript.png
 

JavaScript new operator

 
The JavaScript new special operator is used when we define new instances of custom objects that we create, or when we explicitly define built-in objects like String, Array, Number, Date, Etc.
 
When you create instances of your custom object you must use the new operator in order for the object to be created properly.
 
In this code example below we have created a custom object constructor named character for defining and managing game characters.
 
You can create your custom objects to have any properties you wish, and also apply custom methods to them.
  1. <script>  
  2.     function character(name, type, age, strength, charisma, wisdom)  
  3. {  
  4.     this.name = name;  
  5.     this.type = type;  
  6.     this.age = age;  
  7.     this.strength = strength;  
  8.     this.charisma = charisma;  
  9.     this.wisdom = wisdom;  
  10. }  
  11. var player1 = new character("Brutus""Fighter", 32, 10, 5, 6);  
  12. var player2 = new character("Sticky Fingers""Thief", 25, 7, 8, 8);  
  13. var player3 = new character("Abracadabrus""Wizard", 67, 4, 6, 10);  
  14. var playerArray = [player1, player2, player3];  
  15. document.write("<h1>Adventuring Party</h1>");  
  16. for (var player in playerArray)  
  17. {  
  18.     document.write("<h3>"+playerArray[player].name+"</h3>");  
  19.     document.write("Type : "+playerArray[player].type+"</br>");  
  20.     document.write("Age : "+playerArray[player].age+"</br>");  
  21.     document.write("Strength : "+playerArray[player].strength+"</br>");  
  22.     document.write("Charisma : "+playerArray[player].charisma+"</br>");  
  23.     document.write("Wisdom : "+playerArray[player].wisdom+"</br>");  
  24. }  
  25. </script> 
new-operator-in-javacsript.png
 

JavaScript in operator

 
The JavaScript in special operator is used to see if a property is available in an object, or to check if an array has a specific index number available in it.
 
It will return a value of true if the property exists in the object scope, and returns false if the property cannot be found in the object.
  1. <script>  
  2.     var adam = new Object();  
  3.     adam.quote = "I love to party";  
  4.     adam.age = 72;  
  5.     adam.nature = "Chaotic Neutral";  
  6.     if ("nature" in adam)  
  7.     {  
  8.       document.write("That property is there: " + adam.nature);  
  9.     }  
  10.     else  
  11.     {  
  12.       document.write("That property is NOT in the object scope.");  
  13.     }  
  14. </script>  
  15.   
  16. in-operator-1-in-javascript.png  
  17. Here is an example using the in operator to see if an array index exists.  
  18. <script>  
  19.     var girls = new Array("Lisa""Michelle""Tracy");  
  20.     document.write((0 in girls) + "<br />");  
  21.     document.write((1 in girls) + "<br />");  
  22.     document.write((2 in girls) + "<br />");  
  23.     document.write((3 in girls) + "<br />");  
  24.     document.write((4 in girls) + "<br />");  
  25. </script> 
in-operator-2-in-javascript.png
 

JavaScript delete operator

 
The JavaScript delete special operator is used to delete user-defined properties of custom created objects.
 
It can also delete values from specified indexes of an array, but it will not remove the array element itself.
 
To remove array elements completely use the array shift() method.
 
Delete properties from user-created custom objects
  1. <script>  
  2.     var adam = new Object();  
  3.     adam.quote = "I love dorky stuff";  
  4.     adam.nature = "Evil";  
  5.     document.write(adam.nature + "<hr />");  
  6.     delete adam.nature  
  7.     document.write(adam.nature);  
  8. </script> 
delete-properties-in-javascript.png
 
Delete array values at specified indexes
  1. <script>  
  2.     var girls = new Array("Lisa""Michelle""Tracy");  
  3.     document.write(girls + " | Array Length: " + girls.length);  
  4.     document.write("<hr />");  
  5.     delete girls[0];  
  6.     delete girls[1];  
  7.     document.write(girls + " | Array Length: " + girls.length);  
  8. </script> 
delete-array-in-javascript.png
 

JavaScript typeof operator

 
The JavaScript typeof special operator is used to return the data type of an object.
 
You can also access the constructor property of objects to see the constructor function they use.
  1. <script>  
  2.     var v1 = "Welcome Partner";  
  3.     var v2 = 50;  
  4.     var v3 = new Object();  
  5.     var v4 = Function();  
  6.     var output = "<h3>Determine object types</h3>";  
  7.     output += "v1 • " + typeof (v1) + " • " + v1.constructor + "<br />";  
  8.     output += "v2 • " + typeof (v2) + " • " + v2.constructor + "<br />";  
  9.     output += "v3 • " + typeof (v3) + " • " + v3.constructor + "<br />";  
  10.     output += "v4 • " + typeof (v4) + " • " + v4.constructor + "<br />";  
  11.     document.write(output);  
  12. </script> 
typeof-operator-in-javascript.png
 
JavaScript instanceof operator
 
The JavaScript instanceof special operator evaluates whether or not the left operand explicitly matches the data type you specify as the right operand in the expression.
  1. <script>  
  2.     var myVar = new String("abcdefghi");  
  3.     if (myVar instanceof String)  
  4.     {  
  5.       document.write("Yes it is string type : " + myVar.constructor);  
  6.     }  
  7.   else  
  8.     {  
  9.       document.write("No that is not string type : " + myVar.constructor);  
  10.     }  
  11. </script> 
instanceof-operator-in-javascript.png
 

JavaScript void operator

 
The JavaScript void special operator will run an expression but give no return value.
 
You can see the contrast by running an expression without void first, then run the same expression using void.
 
It processes the expression but where there is normally a return value, it is void.
  1. <script>  
  2.     var a = 2;  
  3.     var b = 3;  
  4.     document.write(c = a + b);  
  5.     document.write("<br />" + c);  
  6.     document.write("<hr />");  
  7.     var c = 2;  
  8.     var d = 3;  
  9.     document.write(void (e = c + d));  
  10.     document.write("<br />" + e);  
  11. </script> 
void-operator-in-javascript.png
 

Summary

 
This article provides much more practical knowledge than theoretical knowledge.