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.

Operator What 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?”

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as