JavaScript tutorial:
JavaScript operators

The examples below give you the commonly used operators that you come across. For the complete list, click here.
To run the code, paste it into JavaScript Editor, and click the Execute button.

Computational

Multiplication (*)

The (*) operator multiplies two numbers.

document.write (1*5); // returns the value 1*5= 5

Division (/)

The (/) operator is use to divide two numbers and return a numeric result.

document.write (100/10); // returns the value 100/10 = 10

Addition (+)

The (+) operator is use to sum two numbers or perform string concatenation.

document.write (7+3); // returns the value 7+3 = 10

Subtraction (-)

The (-) operator used to find the difference between two numbers or to indicate the negative value of a numeric expression.

document.write (10-7); // returns the value 10-7 = 3

Modulo division (%)

The (%) operator Divides two numbers and returns the remainder.

document.write (10%2); // returns the value 10%2 = 0
document.write (11%2); // returns the value 11%2 = 1

Increment/Decrement

The (++) and (--) operators are use to increment or decrement a variable by one.

Increment (++)

a = 10;
a++;


document.write(a); // returns the value 11

Decrement (--)

a = 10;
a--;


document.write(a); // returns the value 9

Logical

Logical And (&&)

The (&&) operator is used to perform the logical conjunction on two expressions. In (A && B) The result is true if both A and B are true.

function MyFunction()
{
var num = prompt("Enter a number between 1 and 10:", "6");

if((num > 5) && (num%2==0))
    alert("Your number is bigger than 5 AND an even number");
else
    alert("Your number is either not bigger than 5 or not an even number");
}

MyFunction();

Logical OR (||)

The (||) operator performs a logical disjunction on two expressions. In (A || B) The result is true if either A or B (or both) are true.

function MyFunction()
{
var num = prompt("Enter a number between 1 and 10:", "6");

if((num > 5) || (num%2==0))
    alert("Your number is either bigger than 5 OR an even number (or both)");
else
    alert("Your number is not bigger than 5 and it is not an even number");
}

MyFunction();

Inequality (!=)
In (A != B) the result is true if A is not equal to B.

function MyFunction()
{
var response = prompt("Is 1/2 bigger than 0.4? Enter YES, or NO", "");

if(response!="YES") alert("The correct answer is YES");
else alert("Your answer was YES, and you were right!");
}

MyFunction();

Equality (==)
In (A == B) the result is true if A is equal to B.

function MyFunction()
{
var response = prompt("Is 1/2 bigger than 0.4? Enter YES, or NO", "");

if(response=="YES") alert("Your answer was YES, and you were right!");
else alert("The correct answer is YES");
}

MyFunction();

To run the code, paste it into JavaScript Editor, and click the Execute button.

Next