JavaScript has more operators other than those stated earlier. Let's go little bit deeper.
If you have an integer and you want to increment it by 1 or any number, you can type the following:
var x = 4; // assigns 4 on the variable x. x = x + 1; /* since x=4, and you are adding 1 with x, so the final value is 4 + 1 = 5, and 5 is stored on the same variable x. */
You can also increment your variable by 1, typing the following:
var x = 4; // assigns 4 on the variable x. x++; // This is similar to x = x + 1.
What will you do if you want to increment your variable by more than 1? Well, you can follow this:
var x = 4; // assigns 4 on the variable x. x = x + 3; // Say, you want to increment x by 3. /* since x = 4, and you are adding 3 with x, so the final value is 4 + 3 = 7, and 7 is stored on the same variable x. */
You can increment your variable by typing the following as well:
var x = 4; // assigns 4 on the variable x. x += 3; // This is similar to x = x + 3.
The output will look similar to the following screenshot on the console:
What about the decrement operator? Yes, you are absolutely right. Decrement operations are same as the increment operations. The only thing that changes is the sign. Your addition (+
) operator will be replaced by the subtraction operator (-
). Let's take a look at an example:
var x = 9; // assigns 9 on the variable x. x = x - 1; /* since x = 9, and you are subtracting 1 from x, so the final value is 9 - 1 = 8, and 8 is stored on the same variable x. */
You can also decrement your variable by 1
typing the following:
var x = 9; // assigns 9 on the variable x. x--; // This is similar to x = x - 1.
What will you do if you want to decrement your variable by more than 1
? Well, you can follow this:
var x = 9; // assigns 9 on the variable x. x = x - 4; // Say, you want to decrement x by 4. /* since x = 9, and you are subtracting 4 from x, so the final value is 9 - 4 = 5, and 5 is stored on the same variable x. */
You can also decrement your variable by typing the following:
var x = 9; // assigns 9 on the variable x. x -= 4; // This is similar to x = x - 4.
The output of these codes can be seen in the following screenshot:
These type of operations are very important for logical operations in JavaScript. You will learn about their uses in Chapter 4, Diving a Bit Deeper.
An assignment operator assigns a value to an operator. I believe that you already know about assignment operators, don't you? Well, you use an equal sign (=
) between a variable and its value. By doing this, you assigned the value to the variable.
Let's take a look at the following example:
var name = "Sherlock Holmes"
The Sherlock Holmes
string is assigned to the name
variable. You have already learned about increment and decrement operators. Can you tell me what will the output of the following codes be?
var x = 3; x *= 2; document.write(x);
The output will be 6.
Do you remember why this has happened?
The x *= 2;
equation is similar to x = x * 2;
as x
is equal to 3
, and later it is multiplied by 2
. The final number (3 x 2 = 6
) is assigned to the same x
variable. That's why we got the following output:
Let's perform the following exercise:
What is the output of the following code?
var w = 32; var x = 12; var y = 9; var z = 5; w++; w--; x*2; y = x; y--; z%2; document.write(" w = "+w+ ", x = "+x+ ", y = "+ y+", z = "+z );
We will get the following output:
w = 32, x = 12, y = 11, z = 5
This output can be seen in the following screenshot:
If you want to do something logical and compare two numbers or variables in JavaScript, you need to use a few logical operators. The following are a few examples of the comparison operators:
Operator |
Description |
---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
=> |
Equal to or greater than |
<= |
Less than or equal to |
The following are a few examples that use these operators:
You will learn more about the use of these operators in the following chapters.
Let's discuss a few bitwise logical operators and bitwise operators:
They might be hard for you to learn right now. Don't worry, you don't have to use them now. We will use them in Chapter 4, Diving a Bit Deeper.
3.145.70.60