How to: JavaScript Calculations

How to: JavaScript Calculations thumbnail
JavaScript facilitates a wide range of calculation types in websites.

Carrying out calculations in JavaScript is generally an easy task. Depending on the aims within your projects, as a developer you may find yourself often carrying out calculations within the JavaScript sections in websites you are working on. One primary advantage to using a programming or scripting language such as JavaScript within websites is that it allows you to use processing such as arithmetic calculations. Common uses for calculations in websites facilitated through JavaScript include Web page calculators.

Instructions

    • 1

      Carry out addition within your JavaScript. The following example code assigns values to two variables, then adds them and assigns the resulting value to a third variable:

      var x = 3;

      var y = 4;

      var result = x + y;//7

      Remember that the equals sign in JavaScript is used to assign values to variables. You can also add one to a number, incrementing it, using the following shorthand syntax:

      var x = 3;

      x++; //4

      This is a common technique in repetitive structures, such as loops.

    • 2

      Carry out subtraction within your code. The following sample JavaScript demonstrates performing subtraction:

      var x = 6;

      var y = 2;

      var result = x - y;//4

      You can also carry out calculations on values directly, rather than assigning them to variables as follows:

      var result = 6 - 2;//4

      To subtract one from a number, decrementing it, you can use the following syntax:

      var x = 6;

      x--;//5

    • 3

      Carry out multiplication in your code. The following extract demonstrates multiplying two variables:

      var x = 5;

      var y = 4;

      var result = x * y;

      The asterisk character is used as the multiplication operator in JavaScript, as the "x" character is simply a text character used in strings. If you use a string within a multiplication operation in JavaScript, it will be converted to a number if possible, so the following would work:

      var x = "5";

      var y = 4;

      var result = x * y;//20

    • 4

      Carry out division in your JavaScript. The following demonstrates division of two variables:

      var x = 10;

      var y = 2;

      var result = x / y;//5

      With any of the arithmetic operators, you can also use the following shorthand, to assign a new value to a variable you are carrying out an operation on in one line:

      var x = 10;

      var y = 2;

      x/=y;//2

      The calculation is performed on the current value of "x," and then the result of this calculation is assigned to "x," overwriting its original value.

    • 5

      Carry out a modulus calculation in your code. You can find out the remainder when dividing two values in JavaScript, using the following code:

      var x = 7;

      var y = 4;

      var result = x % y;//3

      This technique is sometimes used to find out if a numerical value is even or odd, as in the following:

      var x = 3;

      var isOdd = false;

      if((x%2)>0) isOdd = true;

      //isOdd is true in this case

      The conditional test checks whether the remainder after dividing by 2 is greater than zero, in which case the number is odd, with a remainder of 1.

Tips & Warnings

  • If you find the shorthand functions confusing at first, feel free to stick to the techniques that make sense to you for the moment.

  • Variables in JavaScript are weakly typed, so if you perform a calculation on a variable that turns out not to contain a number, your code may fail.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/Photos.com/Getty Images

Comments

You May Also Like

Related Ads

Featured