1. If our assignment operator is =, and x=y, then x=10. 2. If our assignment operator is +=, and x+=y, that translates to x=x+y, so x=30. 3. If our assignment operator is -=, and x-=y, that translates to x=x-y, so x=10. 4. If our assignment operator is *=, and x*=y, that translates to x=x*y, so x=200. 5. If our assignment operator is /=, and x/=y, that translates to x=x/y, so x=2. 6. If our assignment operator is %=, and x%=y, that translates to x=x%y, so x=0.
1. Addition – If our arithmetic operator is +, and x=y+2, then x=9 and y=7. 2. Subtraction – If our arithmetic operator is -, and x=y-2, then x=5 and y=7. 3. Multiplication – If our arithmetic operator is *, and x=y*2, then x=14 and y=7. 4. Division – If our arithmetic operator is /, and x=y/2, then x=3.5 and y=7. 5. Modulus or division remainder – If our arithmetic operator is %, and x=y%2, then x=1 and y=7. 6. Increment – If our arithmetic operator is ++, and x=++y, then x=8 and y=8. Also, if our arithmetic operator is ++, and x=y++, then x=7 and y=8. 7. Decrement – If our arithmetic operator is --, and x=--y, then x=6 and y=6. Also, if our arithmetic operator is --, and x=y--, then x=7 and y=6.
While our examples of arithmetic operators above shows how they work with just simple numerical values, what about different items, such as string variables or text values? The concept is pretty much the same, only with different pieces producing differently formatted results. Let’s take a look at how the arithmetic operator of + can be put to use when you want to combine two string variables or text values. This operator can also be used when you want to combine more than two string variables together.
Once these are executed, our txt3 variable corresponds to "This isan example". You can see that our txt1 and tx2 samples have combined to make this simple phrase, but you can also see that there is no space between the words is and an, and there needs to be one to have it make sense. We can achieve our desired result by simply inserting a space at the end of txt1, right after the word is.
txt1="This is "; txt2="an example"; txt3=txt1+txt2;
While this method will work, you can also achieve the desired result by adding a space into the expression itself:
The space we inserted in both examples gives us the following end result for txt3: "This is an example"
Now it is time to discuss comparison operators
1. If our comparison operator is == (equal to), then x==11 is false, and x==10 is true. 2. If our comparison operator is === (exactly equal to in value and type), then x===10 is true, and x===”10” is false. 3. If our comparison operator is != (not equal to), then x!=11 is true. 4. If our comparison operator is > (greater than), then x>11 is false. 5. If our comparison operator is < (less than), then x<11 is true. 6. If our comparison operator is >= (great than or equal to), then x>=11 is false. 7. If our comparison operator is <= (less than or equal to), then x<=11 is true.
While these examples of comparison operators look completely simple on the surface, they are very useful when you want to compare values in conditional statements and use that comparison to execute a certain action. For instance:
if (price<50) document.write("Too cheap");
This would cause Too cheap to be written if the price is less than 50.
1. If our logical operator is && (and), then (x < 10 && y > 1) is true. 2. If our logical operator is || (or), then (x==5 || y==5) is false. 3. If our logical operator is ! (not), then !(x==y) is true.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.