JavaScript – Operators – Arithmetic – Assignment
Arithmetic operators are used to perform arithmetic between variables and/or values.
Assignment operators are used to assign values to JavaScript variables.
The code:
<!DOCTYPE html> <html> <body> <p>Click the button to calculate x.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { a=4; b=2; // Arithmetic Operators with numbers addition=a+b; // result 6 subtraction=a-b; // result 2 multiplication=a*b; // result 8 division=a/b; // result 2 modulus=a%b; increment=++a; // result increment=5 a=5 increment2=a++; // result increment=4 a=5 decrement=--a; // result decrement=3 a=3 decrement2=a--; // result decrement=4 a=3 // Arithmetic Operators with letters txt1="Hello"; txt2=" World!"; // notice the space before the word txt3=txt1+txt2; // result "Hello World!" // Assignment Operators x=y; // result x=y x+=y; // result x=x+y x-=y; // result x=x-y x*=y; // result x=x*y x/=y; // result x=x/y x%=y; // result x=x%y // Arithmetic Operators with letters and numbers x=5+5; // result 10 y="5"+5; // result 55 -> IT IS A STRING! z="Hello"+5; // result "Hello5!" // Render the first example document.getElementById("demo").innerHTML=addition; } </script> </body> </html>