Operators Task
Task 1
let a = 15;
let b = 4;
Find:
a + b
a % b
program:
let a =15;
let b=4;
console.log (a+b);
console.log (a%b);
output:
19
3
Task 2
let x = 10;
x += 20;
Final value?
program:
let x=10;
x=x+=20; //x=x+20//
console.log(x)
output:
30
Task 3
Output guess:
console.log(5 == "5");
console.log(5 === "5");
program:
console.log(5 == "5");
console.log(5 === "5");
output:
true
false
explanation:
== is only check a value.
=== is check a value and datatype if anyone is different it said false.
Task 4 (Real-life)
let age = 17;
Check:
age >= 18 → print true/false
program:
age=17;
if(age>=18){
console.log("true")
}
else{
console.log("false")
}
output:
false
explanation:
The variable age is initialized with the value 17
The if statement checks whether the condition age >= 18 is true
Since 17 is less than 18, the condition becomes false
When the condition is false, the else block is executed
Therefore, the program prints "false"
Output:
false
Task 5: Even or Odd (Using %)
let num = 7;
Check:
If remainder is 0 → even
Else → odd
Print result
program:
let num = 7;
if (num%2==0){
console.log("even")
}
else{
console.log("odd")
}
output:
odd
🔍 Explanation
The variable num is assigned the value 7
The condition num % 2 === 0 checks whether the number is divisible by 2
If the remainder is 0, the number is even
If the remainder is not 0, the number is odd
Here, 7 % 2 = 1, so the condition is false
Therefore, the else block executes
Output
odd
Key Point
% (modulus) operator is used to find the remainder
Even numbers → remainder 0
Odd numbers → remainder not 0
Task 5: Logical Operators
let age = 22;
let hasID = true;
Check:
age > 18 AND hasID → ?
age < 18 OR hasID → ?
Print both outputs
program:
let age = 22;
let hasID = true;
console.log(age>18 && hasID)
console.log(age<13||hasID)
output:
true
true
what happens?
The variable age is assigned the value 22
The variable hasID is assigned the value true
Condition 1: age > 18 && hasID
age > 18 → 22 > 18 → true
hasID → true
Using AND (&&):
Both conditions must be true
Result:
true && true → true
Condition 2: age < 18 || hasID
age < 18 → 22 < 18 → false
hasID → true
Using OR (||):
At least one condition must be true
Result:
false || true → true
Output
true
true
Key Points
&& (AND) → both conditions must be true
|| (OR) → any one condition is enough
Logical operators are used for decision making
Top comments (0)