Java Script and Operators

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

java script and operators

Arithmetic operators
The Assignment =
We saw that, when declaring a variable, a memory space is reserved for it. Such a space is empty
until you fill it with a value. This is performed with the assignment operation. The assignment
operation gives a value to a variable. It is performed using the = operator. Its syntax is:

VariableName = Value

The VariableName factor must be a valid variable name. It cannot be a value such as a numeric value
or a (double-quoted) string. Here is an example that assigns a numeric value to a variable:
<Script Language="JavaScript">
salary = 12.55;
</Script>
Once a variable has been declared and assigned a value, you can call the document.write() method to
display its value. Since the variable is part of your script and not an HTML tag, it doesn't need to be
included in double-quotes (the proper sentence is that, "it doesn't need to be passed as a string"; but
we have not learned what it means to pass an argument). Here is an example:
<Script Language="JavaScript">
Salary = 12.55;
document.write(Salary);
</Script>
To improve, safeguard, and make your code more efficient, we have learned that you should declare a
variable before using it. This is done using the var keyword. The above script could be written as
follows:
<Script Language="JavaScript">
var Salary;
Salary = 12.55;
document.write(Salary);
</Script>

Exercises Using the Assignment Operator


1. Start your text editor
2. In the empty file, type the following:

<Script Language="JavaScript">
var EarningsJob1, EarningsJob2;

EarningsJob1 = 325.38;
EarningsJob2 = 580.64;

document.write("<p>Salaries</p>");
document.write("Job 1: $");
document.write(EarningsJob1);

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
document.write("<br>");
document.write("Job 2: $");
document.write(EarningsJob2);
</Script>

3. Save the file as operators.htm in the JavaScript Lessons folder


4. Preview the file in the browser and return to your text editor

The Addition +
The addition is an operation used to add one value to another. The syntax used it:

Value1 + Value2

To perform this operation, the interpreter would add the value of Value1 to that of Value2. You can
use such an operation to display the result on an HTML page. Here is an example:

<SCRIPT LANGUAGE="JavaScript">
document.write( 125 + 48);
</SCRIPT>

The addition operation as implemented in JavaScript can be applied to natural numbers called
integers, to floating-point numbers called float or double-precision numbers, or to strings. Here is
an example that illustrates this:

<SCRIPT LANGUAGE="JavaScript">
// The addition operation applied
// to two natural numbers: integers
document.write( 125 + 48);
document.write("<br>");
// to decimal numbers: float or double
document.write(2540.25 + 662.38);
document.write("<br>");
// to two strings
document.write("sekento " + "edward");
</SCRIPT>

You can also apply this operation on variables. For example, once a variable holds a value, you can
add another value to it:

Salary + 0.55

You can also add the values of two variables using their names:

SalaryWeek1 + SalaryWeek2
Inclass exercise : Performing Additions

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
1. To continue with the HTML file, change it as follows:

<Script Language="JavaScript">
var EarningsJob1, EarningsJob2;
var TotalForTheWeek;

EarningsJob1 = 325.38;
EarningsJob2 = 580.64;
TotalForTheWeek = EarningsJob1 + EarningsJob2;

document.write("<p>Salaries</p>");
document.write("Job 1: $");
document.write(EarningsJob1);
document.write("<br>");
document.write("Job 2: $");
document.write(EarningsJob2);

// Although IE can interpret the following line, most other


browsers can't
document.write("<hr align=left width=120 color=#FF5758>");
document.write("Total: $" + TotalForTheWeek);
</Script>

2. Save the file and preview it in the browser

3. After previewing the page, return to your text editor and close

The Multiplication *
The multiplication is used to add a number to itself a certain number of times. This operation is
performed using the * operator. Its syntax is:

Value1 * Value2

This operation can be performed on numeric values as follows:

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
<Script Language="JavaScript">
document.write(15.55 * 240.25)
</Script>

Conditional Operators

Introduction
Conditional statements allow you to control the flow of execution of a script or one of its sections. To
do this, you use some keywords and associate them with expressions to check. Depending on the
outcome of the checking process and other comparison operations, you can take appropriate actions.

Most of the conditional statements perform comparisons and act depending on the outcome of such
comparisons. To assist you in making such comparisons, the JavaScript language is equipped with
special operators that can act on natural numbers, decimal numbers, or strings.
Equality ==
We previously used the assignment operator = to give a value to a variable. Although the assignment
operator works on two operands, one on the left and another on the right of the operator, it doesn't
mean that both operands are equal. It is only used to give a new value, the right value, to the left
variable. Because of that, the left operand must never be a numeric value, although both operands can
be variables.

If you want to find out whether two variables hold the same value, you should use the JavaScript
equality operator. This is performed with == and its syntax is:

Variable1 == Variable2

To perform this operation, the browser (actually the interpreter) compares the operands on both sides
of the operator. If both operands hold the same value, the comparison renders a value of true.

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
Otherwise, the comparison renders false.

Here is an example:

<script language="JavaScript">
document.write("Comparison of 15 == 32 produces ", 15 == 32);
</script>

The Logical Not Operator !


When a variable is not being used or is not available for processing, to make a variable (temporarily)
unusable, you can nullify its value. To render a variable unavailable during the evolution of a
program, apply the logical not operator which is !. Its syntax is:

!Value

There are two main ways you can use the logical not operator. As we will learn when studying
conditional statements, the most classic way of using the logical not operator is to check the state of a
variable.

To nullify a variable, you can write the exclamation point to its left. When used like that, you can
display its value using the cout extractor. You can even assign it to another variable.

When a variable holds a value, it is "alive". To make it not available, you can "not" it. When a
variable has been "notted", its logical value has changed. If the logical value was true, which is 1, it
would be changed to false, which is 0. Therefore, you can inverse the logical value of a variable by
"notting" or not "notting" it.

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
Inequality !=
To compare two variables in order to find out whether they are different, you can use the inequality
operator != whose syntax is:

Variable1 != Variable2

When performing this operation, the interpreter compares the values of Variable1 and Variable2. If
their values are different, which means that they are not equal, the comparison results in a true value
(very important to understand). If they are equal, the result of the comparison is false (observe the
contrast with the equality operator ==).

Here is an example:

<script language="JavaScript">
document.write("Comparison of 15 != 32 produces ", 15 != 32);
</script>

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
Less Than <
If you want to find out whether one value is less than another, use the "less than" operator <. Its
syntax is:

Variable1 < Variable2

The interpreter compares the values held by Variable1 and Variable2. If the value held by Variable1
is less than that of Variable2, the comparison would produce a true value. Otherwise, the result is
rendered false.

Here is an example:

<script language="JavaScript">

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
document.write("Comparison of 15 < 32 produces ", 15 < 32);
</script>

Less Than Or Equal <=


The previous two operations can be combined to compare two values. This allows you to know if two
values are the same or if the first is less than the second. The operator used is <= and its syntax is:

Value1 <= Value2

The <= operation performs a comparison as any of the last two. If both Value1 and Value2 hold the
same value, the result is true or positive. If the left operand, in this case Value1, holds a value lower
than the second operand, in this case Value2, the result is still true.

Here is an example:

<script language="JavaScript">
document.write("Comparison of 15 <= 32 produces ", 15 <= 32);
</script>

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
Greater Than >
When two variables of the same type are distinct, one of them is usually higher than the other. You
can use a logical operator that allows you to find out if one of two values is greater than the other.
The operator used for this operation uses the > symbol. Its syntax is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables or the left operand can be a variable
while the right operand is a constant. If the value on the left of the > operator is greater than the value
on the right side or a constant, the comparison produces a true or positive value . Otherwise, the
comparison renders false or null.

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com
Greater Than or Equal >=
The greater than or the equality operators can be combined to produce an operator as follows: >=.
This is the "greater than or equal to" operator. Its syntax is:

Value1 >= Value2

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of
Value2 are the same, the comparison produces true. If the value of the left operand is greater than that
of the right operand, the comparison produces true also. If the value of the left operand is strictly less
than the value of the right operand, the comparison produces a false or null result.

Here is an example:

<script language="JavaScript">
document.write("Comparison of 15 >= 32 produces ", 15 >= 32);
</script>

Here is a summary table of the logical operators we have studied:

Operator Meaning Example Opposite


== Equality to a == b !=
!= Not equal to 12 != 7 ==
< Less than 25 < 84 >=
<= Less than or equal to Cab <= Tab >
> Greater than 248 > 55 <=
>= Greater than or equal to Val1 >= Val2 <

THIS NOTES SUPPLEMENTS THE CLASS NOTES …… THAT WE HAVE ….. EVANS …. ALL THE BEST …. FOR
QUESTIONS ASK AT OUR GOOGLEGROUP OR SEND A MAIL sundayfeb29@gmail.com

You might also like