Day 63: Operators and expressions

Arithmetic operators

Arithmetic operators are used in JavaScript to perform mathematical operations on numeric values. Here are the basic arithmetic operators in JavaScript:

  1. Addition (+) – Adds two or more values together.
var x = 5;
var y = 3;
var z = x + y; // z is now equal to 8

  1. Subtraction (-) – Subtracts one value from another.
var x = 5;
var y = 3;
var z = x - y; // z is now equal to 2

  1. Multiplication (*) – Multiplies two or more values together.
var x = 5;
var y = 3;
var z = x * y; // z is now equal to 15

  1. Division (/) – Divides one value by another.
var x = 6;
var y = 3;
var z = x / y; // z is now equal to 2

  1. Modulus (%) – Returns the remainder of a division operation.
var x = 7;
var y = 3;
var z = x % y; // z is now equal to 1 (because there's a remainder of one when you divide seven by three)

  1. Increment (++) – Increases the value of a variable by one.
var x = 5;
x++; // The value of x is now six

  1. Decrement (–) – Decreases the value of a variable by one.
var x = 5;
x--; // The value of x is now four

You can also use parentheses () to group operations and control the order of operations, just like in algebra:

// This returns the result of adding three and five, then multiplying that result by two:
var result1 = (3 + 5) * 2;

// This returns the result of subtracting five from ten, then dividing that result by three:
var result2= (10 - 5) /3 ;

Overall, understanding arithmetic operators in JavaScript is essential for performing mathematical calculations and creating dynamic programs that respond to user input and provide useful output.

Comparison operators

Comparison operators are used in JavaScript to compare two values and return a Boolean value (true or false) based on whether the comparison is true or false. Here are the basic comparison operators in JavaScript:

  1. Equal to (==) – Returns true if two values are equal, regardless of their data type.
var x = 5;
var y = "5";
var z = x == y; // z is now equal to true

In this example, even though x is a number and y is a string, the == operator returns true because their values are equal.

  1. Not equal to (!=) – Returns true if two values are not equal, regardless of their data type.
var x = 5;
var y = "6";
var z = x != y; // z is now equal to true

In this example, the != operator returns true because the values of x and y are not equal.

  1. Strictly equal to (===) – Returns true if two values are both equal AND have the same data type.
var x = 5;
var y = "5";
var z = x === y; // z is now equal to false

In this example, the === operator returns false because even though the values of x and y are both five, they have different data types (number and string).

  1. Strictly not equal to (!==) – Returns true if two values are either not equal OR have different data types.
var x = 5;
var y = "6";
var z = x !== y; // z is now equal to true

In this example, the !== operator returns true because even though the values of x and y have different data types (number and string), they are also not equal.

  1. Greater than (>) – Returns true if one value is greater than another value.
var x = 7;
var y = 3;
var z = x > y; // z is now equal to true

  1. Less than (<) – Returns true if one value is less than another value.
var x = 7;
var y = 3;
var z = x < y; // z is now equal to false

  1. Greater than or equal to (>=) – Returns true if one value is greater than or equal to another value.
var x = 7;
var y = 3;
var z1= x >=7 ; //z1 will be True as X has more or equals then Y.
 var z2= x >=10 ;//z2 will be False as X has less then Y.

  1. Less than or Equal To(<=)-Returns True when one Value smaller then other Value
 var num1=10,num2=20,num3=10,num4="20";
   console.log(num1 <=num2);      //Output:True As num1<=num2 
    console.log(num1 <=num3);     //Output:True As num1<=num3 
    console.log(num2 <=num4);      //Output:True As num2<num4  

Overall, understanding comparison operators in JavaScript can help you make decisions in your programs based on how different variables relate to each other.

Logical operators

Logical operators are used in JavaScript to combine multiple conditions and return a Boolean value (true or false) based on whether the combination is true or false. Here are the basic logical operators in JavaScript:

  1. AND (&&) – Returns true if both conditions are true.
var x = 5;
var y = 3;
var z = x > 0 && y < 10; // z is now equal to true

In this example, the && operator returns true because both conditions (x > 0 and y < 10) are true.

  1. OR (||) – Returns true if at least one of the conditions is true.
var x = 5;
var y = 3;
var z = x > 0 || y > 10; // z is now equal to true

In this example, the || operator returns true because at least one of the conditions (x > 0 and y > 10) is true.

  1. NOT (!) – Reverses the result of a condition (returns false if the condition is true, and vice versa).
var x = 5;
var y = !(x == 5); // y is now equal to false

In this example, the (x == 5) condition would normally return true, but by adding the ! operator before it, we reverse its result to get false.

You can also use parentheses () to group conditions and control their order of evaluation:

// This returns true if either (a) x is greater than zero AND less than ten,
// or (b) y is greater than ten:
var result1 = (x > 0 && x < 10) || y > 10;

// This returns false if either (a) x equals zero OR (b) y equals zero:
var result2= !(x == 0 || y ==0);

Overall, understanding logical operators in JavaScript can help you build more complex conditional statements that allow your programs to respond more intelligently to user input and other events.

Exercise: Operators and expressions

Write a program that checks if a given number is even or odd

Write a program that calculates the area of a triangle