Assignment Operators
Assignment operator assigns value to a
variable based on the evaluated value of a specified expression. The basic
symbol used for assignment operator is an equal sign(=). For example, x =
10 assigns 10 to variable x.
You can use also shorthand assignment operator, like the following:
x += y is equivalent to x = x + y
x -= y is equivalent to x = x - y
x *= y is equivalent to x = x * y
x /= y is equivalent to x = x / y
The basic computational operators are addition(+), substraction(-), multiplication(*) and division(/). These operators take the numerical quantities of the operands and perform the operation that evaluates to a single numerical value. Remember that in addition if one of the operands is a string value, string concatenation is performed rather than adding their numerical values.
Other computational operators used in JavaScript are modulus(%), increment(++) and decrement(--).
Logical Operators
Logical operators are used to carry out some
conditional test. The following are the logical operators used in JavaScript:
== and !=
<, <=, > and
>=
&&, || and !
? : The comparison operators(<, <=, > and
>=) can be used in both numeric and string type. If comparing
the numbers, they perform comparisons on the numerical quantities of the
operands. If the operands are of string type, they perform comparisons according
to dictionary order. For example, "click" < "press" returns
true.
Boolean logical operators are used to compare Boolean values and return
Boolean value. The Boolean logical operators are logical
AND(&&), logical OR(||) and logical
NOT(!). The table below shows the returned value of the Boolean
logical operators based on the Boolean values of the operands:
| Expression | exp1 | exp2 | Returned Value |
|---|---|---|---|
exp1 && exp2 |
true | true | true |
exp1 && exp2 |
false | true | false |
exp1 && exp2 |
true | false | false |
exp1 && exp2 |
false | false | false |
exp1 || exp2 |
true | true | true |
exp1 || exp2 |
false | true | true |
exp1 || exp2 |
true | false | true |
exp1 || exp2 |
false | false | false |
!exp1 |
false | true | |
!exp1 |
true | false |
| Symbols | Description |
|---|---|
| + | Addition, String concatenation |
| - | Subtraction, Unary negation |
| * | Multiplication |
| / | Division |
| ++ | Pre-increment, Post-increment |
| -- | Pre-decrement, Post-decrement |
Pre-increment and Post-increment
JavaScript uses a unary operator
++ that adds one(1) to operand. There are two ways you can use the
increment operator; pre-icrement and post-increment.
If this operator is placed before the operand like ++x, it's
called pre-increment operator. It returns the value of x after adding 1 to it.
For example, the initial value of x is 4, ++x sets x to 5 and
returns 5.
If this operator is placed after the operand like x++, it's
called post-increment opearator. It returns the value of x before adding 1 to
it. For example, the initial value of x is 4, x++ sets x to 5 and
returns 4.
Conditions are logical expressions that use comparison and logical operators and evaluate to a Boolean value of true or false
Common comparison used in this course:

Simple conditions use comparison operators to compare numbers and strings and numerical and string expressions.
- Compound Conditions use logical operators to combine logical conditions.
- Common logical operators used in this course:
| if-else
The if-else statement is one way of testing conditions in your program. Below you'll see the format for using if-else. If condition A is true, statementsA will be performed. Then, program flow will jump over all other else conditions and execute statementsD. If conditionA is false, conditionB will be tested. If true, statementsB will be performed, after which statementsD will be performed. If both conditionA and conditionB fail, statementsC will be performed, then statementsD. You can have multiple else if conditions or can omit both the else ifs and the else altogether.
Below you'll see an example of an if-else statement. Try changing the value of the divisor to see how the program behaves differently when it equals 0 or not.
|