0% found this document useful (0 votes)
9 views30 pages

Operators

It teaches some of the Basic Operators u should know

Uploaded by

vimvimtae96
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)
9 views30 pages

Operators

It teaches some of the Basic Operators u should know

Uploaded by

vimvimtae96
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

Operators

Javascript language supports following type of operators.


 Arithmetic Operators
 Comparison Operators
 Logical (or Relational) Operators
 Assignment Operators
 Conditional (or ternary) Operators
Arithmetic Operators
The Javascript arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result

+ Addition x+y Sum of x and y

- Subtraction x-y Difference of x and y

* Multiplication x*y Product of x and y

/ Division x/y Quotient of x and y

% Modulus x%y Remainder of x divided by y

** Exponentiation x ** y Result of raising x to the y'th power


ARITHMETIC OPERATORS
- JavaScript supports various operators:
ARITHMETIC OPERATORS
ASSIGNMENT OPERATORS:
Assignment Same as... Description

x=y x=y The left operand gets set to the value of the
expression on the right

x += y x=x+y Addition

x -= y x=x-y Subtraction

x *= y x=x*y Multiplication

x /= y x=x/y Division

x %= y x=x%y Modulus
ASSIGNMENT OPERATORS:
Increment Decrement
var x = 0; var x = 0;
x++; - equivalent to increment by 1 x--; - equivalent to decrement by 1
x = x +1; - equivalent to increment by 1 x = x - 1; - equivalent to decrement by 1
x+=1; - equivalent to increment by 1 x-=1; - equivalent to decrement by 1
triangle.html
Find the are of the triangle.
Display the output directly on the web page

height = 26;
base = 50;

a = (height * base) / 2;
The Output should be look like this
Area of the triangle is 650
Display the output directly on the web page

Exercise 1. Your parents gave you 500 pesos, you spend 50 pesos on lunch, 50 pesos on
dinner and earn 200 from your part-time job. Calculate how much money you have and
display.

Output
You have total of _______ pesos
total_tax.html
Display the output directly on the web page

Exercise 1. You order a tumbler for 300 pesos, notebook for 100 pesos, and ballpen for 20
pesos. Calculate the total cost of your order and display.

Exercise 2. Calculate the 12% tax for the total in exercise 1.

Output
Total amount spent _______ pesos
12% tax on the total spent _____ pesos
total_tax.html
Display the output directly on the web page

Exercise 1. Your parents gave you 500 pesos. You spent 50 pesos on lunch, 50 pesos on
dinner, and 200 pesos on groceries. Calculate the total amount you spent and display it.

Exercise 2. Calculate the 11% tax for the total you spend in exercise 1.

Output
Total amount spent _______ pesos
11% tax on the total spent _____ pesos
Comparison Operators
The comparison operators are used to compare two values Those values can be numbers or strings.

Operator Name Description Example


== Equal to Return true if both operands are equal; (A == B)
otherwise, it returns false.

!= Not equal to Return true if both operands are equal; (A != B), (A <> B)
otherwise, it returns false.

=== Identical to Return true if both operands have the (A === B)


same data type and equal; otherwise,
it returns false.

!== Not identical to Return true if both operands are not (A !==B)
equal or not have the same data type;
otherwise, it returns false.
Comparison Operators
The comparison operators are used to compare two values Those values can be numbers or strings.

Operator Name Description Example


> Greater than Return true if the operand on the left is greater (A > B)
than the operand on the right; otherwise, it
returns false.
>= Greater than Return true if the operand on the left is greater (A >= B)
or equal to than or equal to the operand on the right;
otherwise, it returns false.
< Less than Return true if the operand on the left is less (A < B)
than the operand on the right; otherwise, it
returns false.
<= Less than or Return true if the operand on the left is less (A <= B)
equal to than or equal to the operand on the right;
otherwise, it returns false.
1. Write a program to check whether a given number is
positive or negative. Document.write
x =2
Output : positive

x = -2
Output : negative
odd_even.html
Using the if..else statement and the Arithmetic Operator (Modulus %).
Identify if the given variable value is Odd or Even. Alert box
Sample
x =2 x =24 x=3 x=9

Output Output Output Output


Even Even Odd Odd
Arith_compare.html
Create a Program that will make a comparison between two
variables. If the first number is greater than the second, perform a
division operation otherwise multiplication operation.
Document.write
a = 10 a=2
b= 2 b= 10

Output Output
10 / 2 = 5 2 * 10 = 20
Logical Operators
The logical operators are used to combine conditional statements.

AND OR
X Y !X !Y
(&&) (||)
T T 1. 5. 9. 13.

F T 2. 6. 10. 14.

T F 3. 7. 11. 15.

F F 4. 8. 12. 16.
Note:
AND and && is the same.
OR and || is the same.
Logical Operators
The logical operators are used to combine conditional statements.

X Y AND && OR || !X !Y

T T T T T T F F

T F F F T T F T

F T F F T T T F

F F F F F F T T

Note:
AND and && is the same.
OR and || is the same.
Logical Operators
The logical operators are used to combine conditional statements.

Operator Name Result

and And True if both x and y are true

or Or True if either x or y is true

&& And True if both x and y are true

|| Or True if either x or y is true

! Not True if x is not true

Note:
AND and && is the same.
OR and || is the same.
Ternary Operator ( ?: )
ACTIVITY
1. Create a HTML file with a webpage title of JavaScript Function and Arithmetic Operators

2. Add script tag to the HTML body

3. Declare 2(two) number variables (integer or float numbers) with unique variable names.

4. Create functions that will compute and return the sum, difference, product, and quotient

of the two assigned numbers.

5. Call each function and print the return values using the following:

a. For addition and multiplication use document.write

b. For subtraction use console.log

c. And for division, use alert box


Activity
Using the if..else statement identify if the assign number in
the variable is Odd or Even number using the Modulus
Operator.
var GivenNumber = 10;
Create a Program to Find the minimum of two numbers. Document.write

x = 37
y = 52

OUTPUT:

The min value is 37

Sample
let num = 2
let numValue = (num%2 == 0) ? “Even” : “Odd”
console.log(numValue)
Using the if else statement, write a program that will check if
a person is eligible to vote or not . document.write

Nationality Age Output


Filipino 17 and below Not eligible
18 and above Eligible
Other Not
Nationality Applicable
Activity
Write a program will prompt the user to enter a student grade
and check the grade using if…else..if..else statement.
GradeRemarks.html
Conditions:
• If grade are between 90% to 100%, the alert box will display Excellent.
• If grade are between 80% to 89%, the alert box will display Very Good.
• If grade are between 70% to 79%, the alert box will display Good.
• If grade are less than 69%, the alert box will display Poor.

Student Grade Display


90-100 Excellent
80-89 Very Good
70-79 Good
69 and below Poor
Other value Invalid Input
BMI
BMI = weight(kg) / (height(m) * height(m))

Console OUTPUT:

Weight(kg): 60
Height(m): 1.7
BMI Result: 20.761245674740486
BMI
BMI = weight(kg) / (height(m) * height(m))

BMI Display Descriptive Information


16-18.4 Underweight
18.5-24.9 Normal
25.0 – 40.0 Overweight

OUTPUT:
Weight(kg): 60
Height(m): 1.7
BMI Result: 20.76
Descriptive Result: Normal

You might also like