 ×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

# How to Work with JavaScript Operators

This article will cover the JavaScript Operators. I will explain mathematical, logical, comparison, assignment and conditional operators using examples.

### Introduction:

This tutorial will cover one of the most important sections of JavaScript called operators. You will learn from this tutorial, how to work with mathematical, logical, comparison, assignment and conditional operators.

First of all we will know the definition of operators. Basically operators are symbol that is used to perform an operation. Example 8+8=16, in this mathematical operation “+” is operators and 8 is operands. Operators in JavaScript are very similar to operators that available in other programming languages like C++ and java. Listed section will cover each operator.

### Mathematical Operators:

In JavaScript, arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value as explained above. There are four standard arithmetic operators, addition (+), subtraction (-), multiplication (*), and division (/) which will be explained programmatically later in a script.

Except the division (/) operator which returns a floating-point division in JavaScript, all operators work as they do in other programming languages. Following listed script will compile the idea of this theoretical concept:

Listing 1: Script of Mathematical Operator
```<!DOCTYPE html>
<title>JavaScript + operator example</title>
<body>
<script type="text/javascript">
var value1 = 19998
var value2 = 10000.09
var line = "<br />"
document.write("value1 + value2 = ")
var result = value1 + value2
document.write(result)
document.write(line)

document.write("value2 * value2 = ")
result = value2 * value2
document.write(result)
document.write(line)

document.write("value2 / value1 = ")
result = value2 / value1
document.write(result)
document.write(line)

document.write("value2 - value1 = ")
result = value2 - value1
document.write(result)
document.write(line)

document.write("value2 % value2 = ")
result = value1 % value2
document.write(result)
document.write(line)

</script>
</body>
</html>
``` Figure1: above figure is output of arithmetic operator in which five operator are described.

### Comparison Operator:

earlier we have seen a single equal (=) sign which sets a value, here you will see double equal sign (==) that used of comparing two values or variables. Comparisons are used to check the basically relationship between variables and/or values. Comparison operators are always used inside conditional statements and prints to either true or false. Listed section will clear the concept of comparison operator:

Listing 2: Script of Comparison Operator

```<!DOCTYPE html>
<body>
<script type="text/javascript">
var value1 = 1;
var value2 = "5";
document.write('Result of value1 = 1 and value2 = "5" +<br />');
document.write("if value1 == value2 ? ",value1 == value2);
</script>
<p>Example of comparison opearator in which result is false</p>
</body>
</html>
``` Figure 2: Above figure is output of comparison operator in which after comparing the values result obtained is printed false for this program.

### Logical Operator:

These are the following logical operators supported by JavaScript language:

• First one is “&&” operator which is called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is true.
• Second one is “||” operator which is called Logical OR Operator. If any of the two operands are non zero then condition becomes true. (A || B) is true.
• Third one is “!” operator which is called not operator. When a variable is not being used or is not available for processing we used null operator or logical not operator. If you want to revert the condition, apply the logical not operator variable you can use logical not operator which is “!”. Its syntax is: “!a” or “!value1” following script will clear the concept.

Listing 3: Example of Logical Operator

```<!DOCTYPE html>
<body>
<script type="text/javascript">
value1= prompt('Input a number(enter a number value should between 100 to 500) : ');
if( value1>= 100 && value1<500 )
else
</script>
</body>
</html>
``` Figure 3: Above figure is output of logical operator script which ask for entering the value between 100 and 500. If the value is right then it will print 'Entered number follows the conditions of these logical operations otherwise false.

### Assignment Operator:

Assignment operator allows you to assign values to variables always assigns a value to its left operand based on the value of its right operand. The first operand must be a variable and basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. Example: X=Y means value of Y is assigning in memory of X .listed example will show you how to work with assignment operator.

Listing 4: Example of Assignment Operator

```<!DOCTYPE html>
<body>
<script type="text/javascript">
var value1 = "my name is jack";
var value2 = "my name is rohan";
document.write('Result of value1 = may name is jack and value2 = my name is rohan <br />');
</script>
<p>Example of Assignment operator in which two string is assigned to two separate variables</p>
</body>
</html>
``` Figure 4: Above figure is example of assignment operator script in which two different value is assigned to two separate variable.

### Conditional Operator:

The conditional operator is replacement of if statement. It takes three operands. Condition: An expression that evaluates to true or false. Expression A and Expression B: Expressions with values of any types. If condition is true, the operator returns the value of Expression A; otherwise, it returns the value of Expression B.

For example:

result = (age >= 24) ? "True" : "False"

The statement assigns value "True" to the variable result if age is 24 or more. Otherwise it assigns the value of "False" to result.

Listing 5: Example of Conditional Operator

```<!DOCTYPE html>
<body>
<script type="text/javascript">
age = prompt('Enter Your age : ');
result = (age >=25) ? "You are not valid candidate" : "You are a valid candidate" ;
document.write(result);
</script>
</body>
</html>

``` Figure 5: Above figure is prompt box which asked for entering your age. It calculates that your entered age is less than or greater than 24. After comparing the values result obtained is printed false for this program.

### Conclusion:

Although tutorial have been explained basic operators in JavaScript, but there are few more operators like ternary operator, Bitwise operator and comma operator. Explore these operators with sample applications. As usual if you have any queries please comment. I am Software Developer from India. I hold B.Tech. in Computer Science & Engineering. I am having 2+ years experience of developing Web applications and Websites using HTML, JavaScript, CSS, C#.NET etc..

What did you think of this post?
Services

Related Posts

[Close] See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US\$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US\$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US\$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!