Basic arithmetic operations in JavaScript. Examples open

Basic arithmetic operations in JavaScript. Examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: ES6

Mathematical operations are one of the most basic and versatile functions of any programming language. In JavaScript, numbers are often used in common tasks such as determining the size of a browser window, calculating the final price of a monetary transaction, or spacing between elements in a website document.

Addition:

let x = 5;
let y = 5;
let z = x + y; //10

Subtraction:

let x = 5;
let y = 5;
let z = x - y; //0

Multiplication:

let x = 5;
let y = 5;
let z = x * y;//25

Division:

let x = 5;
let y = 5;
let z = x / y;//1

Division by modulus:

let x = 5;
let y = 2;
let z = x % y;//1

The largest integer less than or equal to 5 that is divisible by 2 is 4, and 5 – 4 = 1.

Power of a number

const x = 3;
const y = 2;
const z = x ** y;//9 - 3 to the 2nd power

Increment

let x = 10;
let z = ++x;//11

++x – increments the value by 1 and then returns the value.
x++ – is a postfix incrementer that first returns a value and then increments it per unit

Decrement

Similar to increment, only instead of adding a unit, it decreases by one:

let x = 10;
let z = --x;//9

It can also be a prefix and a postfix.

The order of operations

As is customary in mathematics, all operations are performed from left to right and differ in priority: first, increment and decrement operations, then multiplication and division, and then addition and subtraction. To change the standard flow of operations, some expressions can be placed in brackets:

let x = 10;
let y = 5 + (6 - 2) * --x;
console.log(y); //41

Comparison operators

Typically, comparison operators are used to test a condition. Comparison operators compare two values and return true or false:

x == 3//return true or false

Also compares two values and their type, and if they are equal, returns true, otherwise returns false:

x === 3
x != 3

Returns true if the value is not equal to 3 and false if it does.

x !== 3

Returns true if the value is not equal to 3 and false if it does. If the data type is different also returns false.

x > 3

Compares two values, and if the first is greater than the second, then returns true, otherwise returns false

x < 3

Compares two values, and if the first is less than the second, then returns true, otherwise returns false

 
x >= 3

Compares two values, and if the first is greater than or equal to the second, then returns true, otherwise returns false

x <= 3

Compares two values, and if the first is less than or equal to the second, then returns true, otherwise returns false.

Logical operators are very often used in conditionals constructions. Thanks to logical operators, you can combine two expressions into one more complex one.

Operator “&&“(s) – returns true if both comparison operations return true, otherwise returns false

let income = 1000;
let percent = 10;
let result = income > 500 && percent < 12; 
console.log(result);//true 

“||” (or) – returns true if at least one comparison operation returns true, otherwise returns false:

 
let income = 1000;
let isDeposit = true;
let result = income > 500 || isDeposit == false;
console.log(result);//true

Operator “!” – returns true if the comparison operation returns false:

let income = 1000;
let result1 = !(income > 500);
console.log(result1); //false since income > 500 returns
true

Assignment operations

let x = 3;//assigning a value to a variable

Addition followed by assignment of the result:

let a = 5;
a += 5;//10

Subtraction followed by assignment of the result:

let a = 5;
a -= 5;//0

Multiplication followed by assignment of the result:

let a = 5;
a *= 5;//25

Division followed by assignment of the result:

let a = 5;
a /= 5;//1

Raising to a power and then assigning the result:

let a = 5;
a **= 5;//3125

Getting the remainder of a division followed by an assignment result:

let x = 10;
x %= 3; // similarly x = x % 3
console.log(x); // 1, так как 10 - 3*3 = 1

a &&= b returns b if both a and b are true. If either operand is false, then a is returned. Similarly doing a = a && b:

let x = true;
let y = false;
y &&= x;
console.log(y); // false

let c = false;
let d = true;
c &&= d;
console.log(c); // false

let a = true;
let b = true;
a &&= b;
console.log(a); // true

let e = false;
let f = false;
e &&= f;
console.log(e); // false

a ||= b is equivalent to a = a || b:

let x = true;
let y = false;
y ||= x;
console.log(y); // true

let a = true;
let b = true;
a ||= b;
console.log(a); // true

let c = false;
let d = true;
c ||= d;
console.log(c); // true

let e = false;
let f = false;
e ||= f;
console.log(e); // false
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation