Variables and Operators


 Variables and Operators

JavaScript is a programming language that uses variables and operators to manipulate and store data.

Variables are containers used to store data values. They can be declared using the var, let, or const keyword, followed by the variable name.

Operators, on the other hand, are symbols or keywords used to perform operations on data. They can be used to perform arithmetic, comparison, logical, and other operations.

Arithmetic operators include 
+ (addition), 
- (subtraction), 
* (multiplication), 
/ (division), 
and % (modulus)

Operators, on the other hand, are symbols or keywords used to perform operations on data. They can be used to perform arithmetic, comparison, logical, and other operations.

Arithmetic operators include 
+ (addition), 
- (subtraction), 
* (multiplication), 
/ (division), 
and % (modulus).

Comparison operators include 
== (equal to), 
!= (not equal to), 
< (less than), 
> (greater than), 
<= (less than or equal to), 
and >= (greater than or equal to).

Logical operators include 
&& (logical and), 
|| (logical or), 
and ! (logical not).

Example program to cover all the operators

// Declare and initialize some variables 
let a = 10; 
let b = 5; 
const PI = 3.14159; // Use arithmetic operators 
let sum = a + b; // sum is 15 
let difference = a - b; // difference is 5 
let product = a * b; // product is 50 
let quotient = a / b; // quotient is 2 
let remainder = a % b; // remainder is 0 

// Use comparison operators 
let isEqual = (a == b); // isEqual is false 
let isNotEqual = (a != b); // isNotEqual is true 
let isLessThan = (a < b); // isLessThan is false 
let isGreaterThan = (a > b); // isGreaterThan is true 
let isLessThanOrEqual = (a <= b); // isLessThanOrEqual is false 
let isGreaterThanOrEqual = (a >= b); // isGreaterThanOrEqual is true 

// Use logical operators 
let x = true; 
let y = false; 
let andResult = (x && y); // andResult is false 
let orResult = (x || y); // orResult is true 
let notResult = !x; // notResult is false 

// Print the results to the 
console console.log("Sum: " + sum); 
console.log("Difference: " + difference); 
console.log("Product: " + product); 
console.log("Quotient: " + quotient); 
console.log("Remainder: " + remainder); 
console.log("Is equal? " + isEqual); 
console.log("Is not equal? " + isNotEqual); 
console.log("Is less than? " + isLessThan); 
console.log("Is greater than? " + isGreaterThan); 
console.log("Is less than or equal? " + isLessThanOrEqual); 
console.log("Is greater than or equal? " + isGreaterThanOrEqual); 
console.log("Logical and: " + andResult); 
console.log("Logical or: " + orResult); 
console.log("Logical not: " + notResult);

Try it . . .

Post a Comment

Previous Post Next Post