Home > InDesign Scripts > Great Javascript Assignment Operators

Great Javascript Assignment Operators

There are few Javascript assignment operators that we are already using, and they are really handy. We will just go through some of them really quickly just to refresh our memory🙂 So, let’s start!

Here is list of all available Javascript Assignment Operators:

Assignment Operator Action
= Assigns the value of the second operand to the first operand.
+= Adds 2 numbers and assigns the result to the first.
-= Subtracts 2 numbers and assigns the result to the first.
*= Multiplies 2 numbers and assigns the result to the first.
/= Divides 2 numbers and assigns the result to the first.
%= Computes the modulus of 2 numbers and assigns the result to the first.
&= Performs a bitwise AND and assigns the result to the first operand.
^= Performs a bitwise XOR and assigns the result to the first operand.
|= Performs a bitwise OR and assigns the result to the first operand.
<<= Performs a left shift and assigns the result to the first operand.
>>= Performs a sign-propagating right shift and assigns the result to the first operand.
>>>= Performs a zero-fill right shift and assigns the result to the first operand.

Complete list of Javascript Operators can be found here:
http://www.eecs.umich.edu/~bartlett/jsops.html

At the end, I want to show example how to toggle Boolean variable.

var myBool = Boolean();
myBool = true;

Now, ‘myBool’ is true, so, let’s toggle it:

myBool ^= true;

Now, ‘myBool’ is false, and if we do it again:

myBool ^= true;

Our ‘myBool’ is again true. So, we don’t change our code, just execute it again! It’s so simple and easy! Good example how to use this expression can be found in Marc Autret’s snippet for Show Local Formatting, thanks Marc!

Have fun!😀

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: