0% found this document useful (0 votes)
12 views4 pages

JavaScript Operators

The document provides an overview of JavaScript operators, including expressions, arithmetic, assignment, logical, comparison, conditional, and string operators. It explains how these operators function and their precedence in evaluating expressions. Examples are given for each type of operator to illustrate their usage and results.

Uploaded by

solocheruic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

JavaScript Operators

The document provides an overview of JavaScript operators, including expressions, arithmetic, assignment, logical, comparison, conditional, and string operators. It explains how these operators function and their precedence in evaluating expressions. Examples are given for each type of operator to illustrate their usage and results.

Uploaded by

solocheruic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

OPERATORS FOR JAVASCRIPT EXPRESSIONS

An expression is a combination of operators and operands that evaluates to a single value: either a
number or a Boolean value.
Operators are components of expressions that act on operands to produce single result. They join
operands in expression.
Operands are components of expression that are acted upon by operators to evaluate to a value. It
can be a variable, a constant or an array element.
A statement is an executable line of JavaScript code.
Examples:
if (response == answer) == is an operator while response and answer are operands
(7 >= 5) || (5 > 5) >= , || and > are operators while 7 and 5 are operands
answer = 100; answer and 100 are operands while = is an operator.
document.write(output); Is an example of a statement.

Arithmetic Operators
The standard binary arithmetic operators are the same as those on a basic calculator: addition (+),
subtraction (-), multiplication (*), and division (/). In addition to these basic operators, is the modulus
(%) operator which, as mentioned before, calculates the remainder of dividing its operands.
In addition to these binary operators, there are three unary arithmetic operators that are quite useful:
increment (++), decrement (--) and unary negation (-).
Both the increment and decrement operators can be used in two different ways: before the operand or
after. For example, ++x increments x by one and returns the result while x++ returns x and then
increments the value of x. Similarly, --x decreases the value of x by one before returning a result while
x-- returns the value of x before decreasing it's value by one.
Unary negation works a little differently. The operator must precede its single operand, and the result
is the negation of the operand.
Given that y=5, the table below explains the arithmetic operators:
Operator Description Example Result
+ Addition x=y+2 x=7
- Subtraction x=y-2 x=3
* Multiplication x=y*2 x=10
/ Division x=y/2 x=2.5
% Modulus (division remainder) x=y%2 x=1
++ Increment x=++y x=6
-- Decrement x=--y x=4

Assignment operators
Assignment expressions use assignment operators to assign value to a variable. The typical structure
of an assignment expression is:
variable operator expression

Operator Description

= Assigns value of right operand to the left operand


+= Adds the left and right operands and assigns the result to the left operand
-= Subtracts the right operand from the left operand and assigns the result to the left operand
*= Multiplies the two operands and assigns the result to the left operand
/= Divides the left operand by the right operand and assigns the value to the left operand
Divides the left operand by the right operand and assigns the remainder to the left
%=
operand
The %= operand assigns the modulus to the left operand. That is x %= y is the same as x = x
% y. The modulus is the remainder when two numbers are divided.

For example, given that x=10 and y=5, the table below explains the assignment operators:
Operator Example Same As Result
= x=y x=5
+= x+=y x=x+y x=15
-= x-=y x=x-y x=5
*= x*=y x=x*y x=50
/= x/=y x=x/y x=2
%= x%=y x=x%y x=0

Logical Operators
Logical operators include both binary and unary operators. They take boolean values as operands and
return boolean values, as outlined in the table below.
Operator Description

&& Logical "and"—returns true when both operands are true; otherwise it returns false.
Logical "or"—returns true if either operand is true. It only returns false when both operands
||
are false.
Logical "not"—returns true if the operand is false and false if the operand is true. This is a
!
unary operator and precedes the operand.
In discussing logical operators and expressions, it is necessary to discuss short-circuit evaluation. With
short-circuit evaluation, JavaScript will finish evaluating an expression after evaluating the first (left)
operand, if the first operand provides sufficient information to evaluate the expression. Short-circuit
evaluation uses the following rules:
• false && anything is always false.
• true || anything is always true.
Given that x=6 and y=3, the table below explains the logical operators:
Operator Description Example
&& And (x < 10 && y > 1) is true
|| Or (x==5 || y==5) is false
! Not !(x==y) is true
Because the logical "not" operator (!) takes a single operator, there is no short-circuit evaluation for it.
Comparison Operators
Comparison operators are similar to logical operators in that they return boolean values. Unlike logical
operators, they don't require that their operands be boolean values.
Comparison operators are used to compare the value of the operands for equality as well as a number
of other conditions. The table below lists the comparison operators available in JavaScript.
Operator Description

== Returns true if the operands are equal


!= Returns true if the operands are not equal
> Returns true if the left operand is greater than the right operand
< Returns true if the left operand is less than the right operand
>= Returns true if the left operand is greater than or equal to the right operand
<= Returns true if the left operand is less than or equal to the right operand
In JavaScript, all comparison operators are binary operators.
Comparison operators can be used to compare numbers as well as strings; for instance:
1 == 1 returns true.
3 < 1 returns false.
5 >= 4 returns true.
"the" != "he" returns true.
4 == "4" returns true.

Conditional Operators
Conditional expressions are a little different than the others you have seen so far because a conditional
expression can evaluate to one of two different values based on a condition. The structure of a
conditional expression is:
(condition) ? val1 : val2
The way a conditional expression works is that the condition, which is any expression that can be
evaluated to a boolean value, is evaluated; based on the result, the whole expression either evaluates to
val1 (true condition) or val2 (false condition).
The expression
(day == "Saturday") ? "Weekend!" : "Not Saturday!"
evaluates to "Weekend!" when day is "Saturday". Otherwise the expression evaluates to "Not
Saturday!".

String Operators
We use concatenation operator (+). Concatenation returns the union of two strings so that
"Welcome to " + "Netscape Navigator 2.0!"
evaluates to a single string with the value "Welcome to Netscape Navigator 2.0!" As with numbers,
this can be done with a short cut concatenation operator. For example, if the variable welcome has the
value "Welcome to ", then
welcome += "Netscape Navigator 2.0!";
would assign the string "Welcome to Netscape Navigator 2.0!" to the variable welcome.

Operator Precedence
Operator precedence is the set of rules which determine the order in which these compound
expressions are evaluated.
The operators you have learned are evaluated in the following order (from lowest precedence to
highest):
Assignment operators (= += -= *= /= %=)
Conditional (?:)
Logical or (||)
Logical and (&&)
Equality (== !=)
Relational (< <= > >=)
Addition/subtraction (+ -)
Multiply/divide/modulus (* / %)
Parentheses (())
Based on these rules, the expression
5 + 3 * 2 evaluates to 5 + 6which evaluates to 11. Without these rules, the addition operator would be
evaluated before the multiplication operator and the result would be 16.
Likewise, the expression false || true && false evaluates to false
because the && expression is evaluated to false first, and then the || expression (which becomes false
|| false) evaluates to false.

You might also like