[JavaScript] Numbers

Cadet_42·2021년 7월 27일
0

JavaScript

목록 보기
3/8
post-thumbnail

Basic Operators

console.log(2 + 2);
console.log(4 - 2);
console.log(3 * 2);
console.log(6 / 2);

Exponentitation

Exponentitation is the process of taking quantity b (the base) to the power of another quantity e (the exponent). Or in simpler words: it is the process of multiplying b (the base) by itself as many times as indicated by e (the exponent).

console.log(2 ** 3); // => (2 * 2 * 2) result: 8

Modulo

Modulo (%) is the remainder operator.

// 4 / 2 = 2
console.log(4 / 2);
//With a remainder of 0
console.log(4 % 2);

// 7 / 2 = 3.5
console.log(7 / 2);
//With a remainder of 1
console.log(7 % 2);

// If a number modulus other number is equal to 0
// it is a multiple of "other number"

// 8 is indeed a multiple of 2!
console.log(8 % 2 === 0);
// 9 is NOT a multiple of 2!
console.log(9 % 2 === 0);

Assignment Operators Table

Operator Precedence

In mathematics and computer programming, the order of operations (or operator precedence) is a collection of rules that define which procedures to perform first to evaluate a given mathematical expression.

const i = 10 + (5 * 2 ** 3) / 4 - 6;
//  === 10 + 5 * 8 / 4 - 6 <== start with the exponents (2 ** 3)
//  === 10 + 5 * 2 - 6 <== then multiplication (5 * 8)
//  === 10 + 10 - 6 <== then division (40 / 4)
//  === 10 + 4 <== then addition (10 + 10 )
//  ==> 14 <== and finally finish with subtraction (20 - 6)

profile
안녕하세요! 개발공부를 하고 있습니다. 감사히 배우겠습니다. ;)

0개의 댓글