Day2 - Operators in Javascript
This is the link of Day2 Assignment :
Screenshot :-

Activity 1: Arithmetic Operations
Arithmetic operators are used to perform basic mathematical operations.
Task 1: Addition
let num1 = 7;
let num2 = 4;
let sum = num1 + num2;
console.log(sum); // Output: 11
Here, we add two numbers num1 and num2 and log the result to the console.
Task 2: Subtraction
let sub = num1 - num2;
console.log(sub); // Output: 3
We subtract num2 from num1 and log the result.
Task 3: Multiplication
let mult = num1 * num2;
console.log(mult); // Output: 28
We multiply num1 and num2 and log the result.
Task 4: Division
let div = num1 / num2;
console.log(div); // Output: 1.75
We divide num1 by num2 and log the result.
Task 5: Remainder
let remainder = num1 % num2;
console.log(remainder); // Output: 3
We find the remainder when num1 is divided by num2 and log the result.
Activity 2: Assignment Operators
Assignment operators are used to assign values to variables.
Task 6: Addition Assignment
let task6;
task6 += num1 + num2;
console.log(task6); // Output: NaN (Not a Number)
Since task6 is not initialized, adding a value to it results in NaN.
Task 7: Proper Addition and Subtraction
task6 = 0;
task6 += num1 + num2;
console.log(task6); // Output: 11
task6 -= num1;
console.log(task6); // Output: 4
Here, we initialize task6 to 0, then add num1 and num2 to it. After that, we subtract num1 from task6.
Activity 3: Comparison Operators
Comparison operators are used to compare two values.
Task 8: Greater Than and Less Than
console.log(num1 > num2); // Output: true
console.log(num1 < num2); // Output: false
We compare num1 and num2 using > and < operators.
Task 9: Greater Than or Equal To
num1 = 2;
num2 = 3;
let num3 = 2;
console.log(num1 >= num2); // Output: false
console.log(num1 >= num3); // Output: true
We use the >= operator to compare num1 with num2 and num3.
Task 10: Equality and Strict Equality
let comp1 = 3; // type will be number
let comp2 = "3"; // type will be string
console.log(comp1 == comp2); // Output: true
console.log(comp1 === comp2); // Output: false
The == operator checks for value equality, while the === operator checks for both value and type equality.
Activity 4: Logical Operators
Logical operators are used to combine multiple conditions.
Task 11: Logical AND (&&)
let cond1 = true;
let cond2 = false;
let cond3 = true;
let cond4 = false;
if (cond1 && cond3) {
console.log("Hey Both conditions are true."); // It will print.
}
if (cond1 && cond2) {
console.log("Nothing will be return."); // It will not print as one condition is false.
} else {
console.log("Hey one or two conditions is false."); // It will print.
}
Task 12: Logical OR (||)
if (cond1 || cond3) {
console.log("One of the condition is true."); // It will print.
}
if (cond1 || cond2) {
console.log("One of the conditions should be true."); // It will print.
}
if (cond2 || cond4) {
console.log("Minimum one condition should be true.");
} else {
console.log("Both conditions are false."); // It will print.
}
Task 13: Logical NOT (!)
if (!cond2) {
console.log("Condition is false."); // It will print because !cond2 means cond2 is false.
}
Activity 5: Ternary Operator
The ternary operator is a shorthand for an if-else statement.
Task 14: Check if a Number is Positive or Negative
num1 = 3;
num2 = 4;
let result1 = num1 % 2 == 0 ? "It is even" : "It is odd";
console.log(result1); // Output: It is odd.
let result2 = num2 % 2 != 0 ? "It is odd" : "It is even";
console.log(result2); // Output: It is even.
he ternary operator checks the condition and returns one of two values based on whether the condition is true or false.
Feature Request
1. Arithmetic Operations Script
A script that performs basic arithmetic operations on two numbers and logs the results:
let number1 = 7;
let number2 = 4;
console.log(`Addition: ${number1 + number2}`); // Output: Addition: 11
console.log(`Subtraction: ${number1 - number2}`); // Output: Subtraction: 3
console.log(`Multiplication: ${number1 * number2}`); // Output: Multiplication: 28
console.log(`Division: ${number1 / number2}`); // Output: Division: 1.75
console.log(`Remainder: ${number1 % number2}`); // Output: Remainder: 3
2. Comparison and Logical Operators Script
A script that compares two numbers using different comparison operators and combines conditions using logical operators, logging the results:
let n1 = 3;
let n2 = "3";
console.log(`Equality: ${n1 == n2}`); // Output: Equality: true
console.log(`Strict Equality: ${n1 === n2}`); // Output: Strict Equality: false
let condition1 = true;
let condition2 = false;
console.log(`Logical AND: ${condition1 && condition2}`); // Output: Logical AND: false
console.log(`Logical OR: ${condition1 || condition2}`); // Output: Logical OR: true
console.log(`Logical NOT: ${!condition2}`); // Output: Logical NOT: true
3. Ternary Operator Script
A script that uses the ternary operator to determine if a number is positive or negative and logs the result:
let number = -5;
let check = number >= 0 ? "Positive" : "Negative";
console.log(check); // Output: Negative
About the author
I am a passionate web developer and open-source enthusiast on a captivating journey of coding wonders. With a year of experience in web development, my curiosity led me to the enchanting world of React, where I found a true calling. Embracing the magic of collaboration and knowledge-sharing, I ventured into the realm of open source, contributing to Digital Public Goods (DPGs) for the betterment of the digital universe. A firm believer in learning in public, I share my insights and discoveries through blogging, inspiring fellow coders to embark on their own magical coding odysseys. Join me on this thrilling adventure, where imagination and technology converge, and together, let's shape the future of the digital landscape! 🎩✨
Comments
Join the discussionNo comments yet. Be the first to comment.