JavaScript Operators and Expressions
JavaScript Operators and Expressions
An operator combines the values of its operands in some way and evaluates to a
new value. Operators are used for JavaScript’s arithmetic expressions,
comparison expressions, logical expressions, assignment expressions.
• Arithmetic expressions
• Relational expressions
• Logical expressions
Arithmetic Operators
JavaScript supports all the basic arithmetic operators like addition (+),
subtraction (–), multiplication (*), division (/), and modulus (%, also known as
the remainder operator).
<Html>
<Head>
</Head>
<Body>
</script>
</Body>
</Html>
Output:
Assignment Operator
An assignment operator is the operator used to assign a new value to a variable.
Assignment operator can also be used for logical operations such as bitwise
logical operations or operations on integral operands and Boolean operands.
var number1=10;
var number2=number1;
var booleanvar=true;
The assignment operator can also be used to set a variable to hold the value of
an expression. For example,
JavaScript supports some shorthand arithmetic operators like +=, -=, *=, /= and
%= to evaluate arithmetic calculations.
<Html>
<Html>
<Head>
</Title>
</Head>
<Body>
</script>
</Body>
</Html>
Output:
Assignment:
1. To find Simple Interest for the given Principle, Number of years and Rate of
interest.
2. To find Compund Interest for the given Principle, Number of years and Rate
of interest.
<Html>
<Head>
</Head>
<Body>
document.write("<br>Data1 : "+value1);
document.write("<br>Data2 : "+value2);
</script>
</Body>
</Html>
Output:
Logical Operators:
Usage :
• For && (AND) the result is false if the first operand is false; otherwise, the
result is the Boolean value of the second operand.
• For || (OR) the result is true if the first operand is true; otherwise, the result
is the Boolean value of the second operand.
• For ! (NOT) the result is true if the operand is false; otherwise, the result is
true.
<Html>
<Head>
</Head>
<Body>
<script language="javascript" type="text/javascript">
document.write("<br>Data1 : "+value1);
document.write("<br>Data2 : "+value2);
var res3=(!(value1!=value2));
</script>
</Body>
</Html>
Output:
String Operators:
<Html>
<Head>
</Head>
<Body>
</script>
</Body>
</Html>
Output:
The ++ operator increments its single operand. The operator converts its
operand to a number, adds 1 to that number, and assigns the incremented value
back into the variable. The return value of the ++ operator depends on its
position relative to the operand. When ++ is used before the operand, where it is
known as the pre-increment operator, it increments the operand and evaluates to
the incremented value of that operand. When used after the operand, where it is
known as the post-increment operator, it increments its operand but evaluates to
the un-incremented value of that operand. Consider the difference between
these two lines of code:
var m = 1, n = m++; // m is 2, n is 1
The -- operator decrements its single operand. It converts the value of the
operand to a number, subtracts 1, and assigns the decremented value back to the
operand. Like the ++ operator, the return value of -- depends on its position
relative to the operand. When used before the operand, it decrements and
returns the decremented value. When used after the operand, it decrements the
operand but returns the undecremented value.
<Html>
<Head>
</Head>
<Body>
</Body>
</Html>
Output:
typeof Operator:
The typeof operator is used to get the data type (returns a string) of its operand.
The operand can be either a literal or a data structure such as a variable, a
function, or an object. The operator returns the data type.
Syntax
typeof operand
or
typeof(operand)
typeof returns: boolean, function, number, string, and undefined. The following
table summarizes possible values returned by the typeof operator.
<Html>
<Head>
</Head>
<Body>
<script language="javascript" type="text/javascript">
value3=true;
</script>
</Body>
</Html>
Output:
In the above example, since the condition returns false the value 150 will be
assigned to result.
<Html>
<Head>
</Head>
<Body>
</script>
</Body>
</Html>
Output:
Summary: in this tutorial, you will learn how to use JavaScript comparison
operators to compare two values.
Operator Meaning
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to
A comparison operator returns a Boolean value indicating that the comparison is
true or not. See the following example:
let r1 = 20 > 10; // true
let r2 = 20 < 10; // false
let r3 = 10 == 10; // true
The comparison operator takes at least two values (or operands). If one of the two
values has a different type, JavaScript will perform a conversion based on specific
rules before comparing them. We will discuss each rule in detail in the following
sections.
Comparing numbers
If the operands are numbers, JavaScript will perform a numeric comparison. For
example:
let a = 10,
b = 20;
console.log(a >= b); // false
console.log(a == 10); // true
This example is straightforward. the variable a is 10, b is 20. The a>=b expression
returns and a==10 expression returns true.
Comparing strings
If the operands are strings, JavaScript compares the character codes numerically
one by one in the string.
let f1 = 'apple',
f2 = 'Banana';
let result = f2 < f1;
console.log(result); // true
In this example, f2 is less than f1 because the letter B has the character code 66 while
the letter a has the character code 97.
To fix this, you must first convert strings into a common format, either lowercase
or uppercase and then perform comparison as follows:
If an operand is a number while the other is not, JavaScript converts the non-
numeric operand to a number and performs comparison numerically.
let orange = {
toString: function() {
return '20';
}
};
console.log(apple > 10); // false
console.log(orange == 20); // true
In this first comparison, the apple object has the valueOf() method that returns 10,
therefore, JavaScript uses 10 for comparison. In the second comparison, JavaScript
first calls the valueOf() method. However, the orange object doesn’t have
the valueOf() method so JavaScript calls the toString() method to get the returned value
of 20.
Comparing null and undefined
However, in the second comparison, we use the strict equal operator ( ===),
JavaScript doesn’t convert the string before comparison, therefore the result is false.
In this tutorial, you have learned how to use the JavaScript comparison operators to
compare values.