L3. Operators in JS, CSE 202, BN11
L3. Operators in JS, CSE 202, BN11
Types of
operators Arithmetic Assignment
operator operator
.
2
Types of operators
There are different types of JavaScript operators:
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. String Operators
5. Logical Operators
6. Bitwise Operators
7. Ternary Operators
8. Type Operators
3
Arithmetic operator
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement
4
Arithmetic operator
5
Assignment operator
Operator Example Same As
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
6
Comparison operator
Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
7
Comparison operator
9
logical operator
10
Worked out example
Example 3: Program to take input from prompt and calculate through JS and
display in the browser. (Calculate area of a triangle, take length and height as an
input through prompt and display in the browser.)
11
Interface
12
html codes
<!DOCTYPE html> <body>
<html lang="en"> <div class="container"
<head> id="container">
<meta charset="UTF-8"> <div id="result"></div>
<meta name="viewport"
content="width=device-width, initial- <script src="app.js"></script>
scale=1.0"> </div>
<title>LogicControl</title> </body>
<link rel="stylesheet" href="style.css"> </html>
</head>
13
css codes
margin: 20px auto;
body{
margin: 0px; padding: 10px;
background-color: rgba(90,90,230,0.6); display: flex;
} flex-direction: column;
justify-content: center;
.container{ align-items: center;
background-color: rgba(90,90,230,0.3); border-radius: 10px;
width: 300px; box-shadow: 0 0 3px 1px rgba(0,0,0,0.5);
height: 300px; font-size: large;
14
JS codes
const resultEl =
document.getElementById("result"); function areaCal(){
const length = prompt("Enter length in meter const area = 0.5*length*height;
= "); resultEl.innerText = "Area of the
const height = prompt("Enter height in meter triangle: " + area + " sq. meter";
= "); }
areaCal();
15
Class tasks
Task 3.1: Program to calculate average penetration in mm from three bitumen
samples in the bitumen penetration test. Take penetration values as input and
display average value in the browser. (hints: average = (val1 + val2 +val3)/3)
Task 3.2: Program to calculate area of a rectangular industrial plot. Take width and
breadth as input and display area (sq. ft) in the browser. (hints: areaRectangle = width x
breadth)
Task 3.3: Program to calculate discharge of an irrigation canal. Take cross-section and
water velocity as input and display discharge (cum/sec) in the browser. (hints:
discharge= crossSection x velocity)
Task 3.4: Program to calculate delay in a signalized intersection. Take travel time and
free flow travel time as input and display delay (sec) in the browser.(hints: delay =
travelTime – freeFlowTravelTime)
Task 3.5: Program to calculate maximum bending moment of a simply supported
beam under UDL . Take UDL and span length as input and display moment (kip-ft) in
the browser.(hints: maxMoment = 1/8 x UDL x span x span)
16
End of the Lecture
17