JavaScript calculation base examples

 Here are some examples of basic calculations in JavaScript:

1. Addition

let a = 5;
let b = 3; let sum = a + b;
console.log(sum); // Output: 8

2. Subtraction

let a = 10;
let b = 4; let difference = a - b;
console.log(difference); // Output: 6

3. Multiplication

let a = 6;
let b = 7; let product = a * b;
console.log(product); // Output: 42

4. Division

let a = 20;
let b = 4; let quotient = a / b;
console.log(quotient); // Output: 5

5. Modulus (Remainder)

let a = 15;
let b = 4; let remainder = a % b;
console.log(remainder); // Output: 3

6. Exponentiation

let a = 2;
let b = 3; let power = a ** b;
console.log(power); // Output: 8

7. Square Root

let a = 25;
let squareRoot = Math.sqrt(a);
console.log(squareRoot); // Output: 5

8. Using Parentheses for Order of Operations

let a = 2;
let b = 3; let c = 4; let result = (a + b) * c;
console.log(result); // Output: 20

9. Increment and Decrement

let a = 5;
a++; // Increment by 1
console.log(a); // Output: 6 let b = 10; b--; // Decrement by 1
console.log(b); // Output: 9

10. Handling Floating Point Numbers

let a = 0.1;
let b = 0.2; let sum = a + b;
console.log(sum); // Output: 0.30000000000000004 (floating-point precision issue) let roundedSum = sum.toFixed(2); // Round to 2 decimal places
console.log(roundedSum); // Output: 0.30


JS

No comments:

Post a Comment

Angular's new httpResource and a bonus hidden feature

  The first   release candidate   for   Angular 19.2   is out, and it includes the new experimental   httpResource . This is essentially a s...

Best for you