MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Incrementing

The Increment operator (++) increments its operand by one.

  • If used as a postfix, then it returns the value before incrementing.
  • If used as a prefix, then it returns the value after incrementing.

  • postfix


    var a = 5, 
     b = a++, 
     c = a 

    Output

    In this case, a is incremented after setting b. So, b will be 5, and c will be 6.

    prefix


    var a = 5, 
     b = ++a, 
     c = a 

    Output

    In this case, a is incremented before setting b. So, b will be 6, and c will be 6.

    The increment and decrement operators are commonly used in for loops, for example:


    for(var i = 0; i < 42; ++i)
    {
     // do something awesome!
    }

    Math.pow() or **

    Exponentiation makes the second operand the power of the first operand (ab).


    var a = 2,
     b = 3,
     c = Math.pow(a, b);

    Output

    c will now be 8

    let a = 2,
     b = 3,
    c = a ** b;

    Output

    c will now be 8

    Use Math.pow to find the nth root of a number.

    Finding the nth roots is the inverse of raising to the nth power. For example 2 to the power of 5 is 32. The 5th root of 32 is 2.


    Math.pow(v, 1 / n); // where v is any positive real number
                        // and n is any positive integer
    var a = 16;
    var b = Math.pow(a, 1 / 2); // return the square root of 16 = 4
    var c = Math.pow(a, 1 / 3); // return the cubed root of 16 = 2.5198420997897464
    var d = Math.pow(a, 1 / 4); // return the 4th root of 16 = 2
    


    Conclusion

    In this page (written and validated by ) you learned about Javascript Incrementing . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Addition and Subtraction.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.