devxlogo

JavaScript Operators

JavaScript Operators

his section summarizes the the most common operators you might use in JavaScript scripts. They are grouped by general function. Scroll through the file or jump to the functional category by clicking on one of the category names below.

Assignment

General Comments: The most common assignment operator is the equal sign. It sets one item equal to another.

OperatorWhat does it do?Example/Explanation
=

Sets one value equal to another

counter=0

Sets the counter to equal the number 0

+=

Shortcut for adding to the current value.

clicks += 2

Sets the variable named counter to equal the current value plus two.

-=

Shortcut for subracting from the current value.

clicks -= 2

Sets the variable named counter to equal the current value minus two.

*=

Shortcut for multiplying the current value.

clicks *= 2

Sets the variable named counter to equal the current value multiplied by two.

/=

Shortcut for dividing the current value.

clicks /= 2

Sets the variable named counter to equal the current value divided by two.

Comparison

General Comments:  The comparison operators compare two items and return a value of “true” if the conditions are true.

Operator
What does it do?Example/Explanation
==

Returns a true value if the items are the same

counter==10 >

Returns the value “true” if the counter’s value is currently equal to the number 10

Returns a true value if the items are not the same

counter!=10

Returns the value “true” if the counter’s value is any value except the number 10

>

Returns a true value if the item on the left is greater than the item on the right

counter>10

Returns the value “true” if the counter’s value is larger than the number 10

>=

Returns a true value if the item on the left is equal to or greater than the item on the right

counter>=10

Returns the value “true” if the counter’s value is equal to or larger than the number 10

<

Returns a true value if the item on the left is less than the item on the right

counter<10

Returns the value “true” if the counter’s value is smaller than the number 10

<=

Returns a true value if the item on the left is equal to or less than the item on the right

counter<=10

Returns the value “true” if the counter’s value is equal to or less than the number 10

Computational

General Comments:  The computational operators perform a mathematical function on a value or values, and return a single value.

Operator
What does it do?Example/Explanation
+

Adds two values together

counter+2

Returns the sum of the counter plus 2

Subtracts one value from another

counter-2

Returns the sum of the counter minus 2

*

Multiplies two values

counter*10

Returns the result of the variable times 10

/

Divides the value on the left by the one on the right and returns the result

counter/2

Divides the current value of the counter by 2 and returns the result

++X

Increments the value, and then returns the result

++counter

Looks at the current value of the counter, increments it by one, and then returns the result. If the counter has a value of 3, this equation returns the value of 4.

X++

Returns the value, and then increments the value

counter++

Returns the value of the counter, then increments the counter. If the counter has a value of 3, this equation returns the value of 3, then sets the counter value to 4.

–X

Decreases the value, and then returns the result

–counter

Looks at the current value of the counter, decreases it by one, and then returns the result. If the counter has a value of 7, this equation returns the value of 6.

X–

Returns the value, and then decreases the value

counter–

Returns the value of the counter, then decrease the counter value. If the counter has a value of 7, this equation returns the value of 7, then sets the counter value to 6.

Logical

General Comments:  The logical operators evaluate expressions and then return a true or false value based on the result.

Operator
What does it do?Example/Explanation
&&

Looks at two expressions and returns a value of “true” if both expressions are true.

if day=’friday’&&date=13 then alert(“Are You Superstitious?”)

Compares the value of the day and the value of the date. If it is true that today is a Friday and if it is also true that the date is the 13th, then an alert box pops up with the message “Are You Superstitious?”

||

Looks at two expressions and returns a value of “true” if either one?but not both?of the expressions are true.

if day=’friday’&&date=13 then alert(“Are You Superstitious?”) else if day=’friday’||date=13 then alert(“Aren’t you glad it isn’t Friday the 13th?”)

Compares the value of the day and the value of the date. If it is true that today is a Friday and if it is also true that the date is the 13th, then an alert box pops up with the message “Are You Superstitious?” If both are not true, the script moves onto the next line of code…

Which compares the value of the day and the value of the date. If either one?but not both?is true, then an alert box pops up with the message “Aren’t you glad it isn’t Friday the 13th?”

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist