Pin Me

Advanced Operators in JavaScript

written by: •edited by: Michele McDonough•updated: 4/2/2010

This guide explains all of the advanced arithmetic operators in JavaScript and how they can be used with examples and syntax specifications.

  • slide 1 of 1

    Advanced Operators in Javascript

    JavaScript Operators Now that we are done with the basics of JavaScript and how they can be used with your HTML pages to add interactivity, let's move on to the next step and learn about the various arithmetic operators in JavaScript. We have already learned about the basic arithmetic operators - add, subtract, multiply and divide in the introduction to JavaScript.

    In this tutorial, I will acquaint you with all of the other JavaScript operators.

    Advanced Arithmetic Operators:

    Modulus (%)

    var a = 10;

    var b = 4;

    var c = a%b;

    document.write(c);

    This will print the value of C as 2.

    The modulus operator returns the remainder when the first number is divided by the second. A divided by B returns 2 as the remainder.

    Increment (++)

    This operator operates on just one variable instead of two. It increments or increases the value of the variable by 1.

    var a = 1;

    a++;

    document.write(a);

    This will print the value of a as 2.

    Decrement (--)

    The Decrement operator decrements or reduces the value of the variable by 1.

    var a = 1;

    a--;

    document.write(a);

    This will print the value of a as 0.

    Advanced Assignment Operators:

    (=) is the basic assignment operator. It assigns a particular value to a variable.

    var a = 10;

    This assigns the value 10 to the variable a.

    We can club this with the basic arithmetic operators to give us advanced assignment operators.

    Example:

    var a = 40;

    var b = 20;

    a+=b;

    document.write(a);

    This would print the value of a as 60.

    The compound operator a+=b actually works as a = a+b. So a = 40+20 = 60.

    The basic syntax is var1 operator= var2 which means var1 = var1 operator var2.

    This rule applies to all the basic operators.

    So if

    var a = 40;

    var b = 20;

    a += b would return a = a + b = 60

    a -= b would return a = a - b = 20

    a *= b would return a = a * b = 800

    a /= b would return a = a / b = 2

    a %= b would return a = a % b = 0

    Besides being easier to use, these shorthand operators also have slightly lower processing time, which should give you better JavaScript performance.

    Finally, there is the string concatenation operator ( + ):

    The ~ezentity_quot+ezentity_quot~ operator has been overloaded to function as both the addition operator and the string concatenation operator.

    var text1 = "Hi, ";

    var text2 = "I'm Pat";

    var text3 = text1+text2;

    document.write(text3);

    This will save the concatenated string in the variable text3 and print it - "Hi, I'm Pat"

    When the operator is used with text strings, it functions as a concatenation operator and when it is used with numbers, it works as an addition operator. When it is used with a string and a number, the number gets concatenated with the string.

JavaScript - Tutorials for Beginners

This collection contains several basic JavaScript tutorials for beginners. It explains fundamental concepts, such as operators, variables, loops, events, errors and conditional statements in JavaScript. It assumes no prior programming knowledge.
  1. Conditional Statements in JavaScript
  2. Looping Constructs in JavaScript
  3. Advanced Operators in JavaScript
  4. Alerts and Functions in JavaScript
  5. Error Handling in JavaScript