JavaScript

Java Script Operators

Operators perform operations on variables. 

The following are the JavaScript Operators
Arithmetic Operators are Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Increment(++) and Decrement(–)
Assignment Operators are (=), (+=), (-=), (*=), (/=), (%=)
Bitwise Operators are Bitwise and (&), Bitwise Or(|), Bitwise XOR(^), Bitwise NOT(~), Bitwise left shift(<<), Bitwise right shift(>>)
Comparison Operators are equal to (==), Identical (===), Not equal to (!=), Greater than (>), Less than(<), Greater than or equal to (>=), Less than or equal to(<=)
Logical Operators are Logical and (&&), Logical or (||), Logical not (!)
Relational Operators are in and instanceof
Ternary Operator is (?:)
Unary Operators are typeof, delete and void

Arithmetic Operators returns single numerical value from the operands
Assignment Operators assign values of right operand to the left operand
Bitwise Operators perform bitwise operations on operands
Comparison Operators returns true or false after comparing the left and right operands
Logical Operators return Boolean or non-boolean values based on how they are used
Relational Operators return Boolean values
Ternary Operator returns the first or second value based on the condition evaluated
Unary Operators are operators with one operand

<!DOCTYPE html>

<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>JS Operators</title>
</head>
<body>

<script>

//Arithmetic Operators
document.write(“Arithmetic opeators”)
document.write(“<br>”)
//Addition, Subtraction, multiplication, division
var x=10,y=20,z=100
document.write(((x * y) + z – y) / x) //Answer:28
document.write(“<br>”)
//Exponentiation (2 cubed 3)
var p=2**3
document.write(p) //Answer:8
document.write(“<br>”)
//Modulus (remainder of 37 by 5)
var q = 37 % 5
document.write(q) //Answer:2
document.write(“<br>”)
//Increment and decrement
var r = 10, s = 20
r++
s–
document.write(r) //Answer:11
document.write(“<br>”)
document.write(s) //Answer:19
document.write(“<br>”)

//Assignment Operator “equal” assigns the value to variable
document.write(“<br>”)
document.write(“Assignment operators”)
document.write(“<br>”)
t = x
document.write(t) //Answer:10
document.write(“<br>”)
//Assignment operator can be combined with other operators
//as in example below
a = 1, b = 2
a += b //this means a=a+b
document.write(a) //Answer:3
document.write(“<br>”)

//Comparison operators
document.write(“<br>”)
document.write(“Comparison operators”)
document.write(“<br>”)
//equal to
c = 1, d = 2, m=6, n=”6″
document.write((c == d)) //Answer: false
document.write(“<br>”)
document.write((m == n)) //Answer:true
document.write(“<br>”)
//Compare both value and type with ===
document.write((m === n)) //Answer: false
document.write(“<br>”)
//not equal to
document.write((c != d)) //Answer: true
document.write(“<br>”)
//greater than
document.write((c > d)) //Answer: false
document.write(“<br>”)
//less than
document.write((c < d)) //Answer: true
document.write(“<br>”)
//greater than or equal to
document.write((c >= d)) //Answer: false
document.write(“<br>”)
//less than or equal to
document.write((c <= d)) //Answer: true
document.write(“<br>”)


//ternary operator
let e = (c >= d) ? “c greater than or equal to d” : “c less than or equal to d”
document.write(e)
document.write(“<br>”) //Answer:c less than or equal to d


//Logical operators
document.write(“<br>”)
document.write(“Logical operators”)
document.write(“<br>”)
j = 2, k = 3
document.write(j < 5 && k > 4) //
document.write(“<br>”) //Answer: false
document.write(j < 5 || k > 4)
document.write(“<br>”) //Answer: true
document.write(!(j < 5))
document.write(“<br>”) //Answer: false
</script>
</body>
</html>

Output:
Arithmetic opeators
28
8
2
11
19

Assignment operators
10
3

Comparison operators
false
true
false
true
false
true
false
true
c less than or equal to d

Logical operators
false
true
false