unary 단항의, 1진법의
binary 2진법의, 2항의
operand 피연산 함수 (argument)
5*2 → 5 multiplied by 2 equals 10.
let's grasp some common terminology.
An operand - is what operators are applied to . For instance, in the multiplication of 5 * 2 there are two operands: the left operand is 5 and the right operand is 2. Sometimes, people called these 'arguments' instead of 'operands'.
An operator is unary if it has a single operand.
let x = 1;
x = -x
alert(x); // -1, unary negation was applied
The following math operations are supported:
The result of a % b is the remainder of the integer division of a by b.
a % b 의 결과는 a를 b로 나눈 나머지 정수이다.
The exponentiation operator a b raises a to the power of b**.
= a의 b제곱
⬇️ Let's meet features of JavaScript operators that are beyond school arithmetics.
If the binary + is applied to strings, it merges (concatenates) them:
let s = 'my' + 'string';
alert(s); //
Note that if any of the operands is a string, then the other one is converted to a string too.
alert('1' + 2); // '12'
alert(2 + '1'); // '21'
See, it doesn't matter whether the first operand is a string or the second one.
alert(2 + 2 + '1'); // '41' and not '221'
Here, operators work one after another.
operator들은 연달아 계산을 한다.
앞의 +는 2와 2를 더해 4가 되고 4와 스트링 1을 더해 '41'이 결과로 도출되는 것이다.
alert('1' + 2 + 2); // '122' and not '14'
Here, the first operand is a string, the compiler treats the other two operands as strings too.
첫 번째 operand가 스트링이고, 컴파일러는 나머지 두 operand들도 스트링으로 여긴다.
The binary + is the only operator that supports strings in such a way. Other arithmetic operators work only with numbers and always convert their operands to numbers.
다른 산술 오퍼레이터들은 오직 숫자와만 일하고, 항상 그들의 operands를 숫자로 변환한다.
alert(6 - '2'); // 4
alert('6' / '2'); // 3, converts both operands to numbers
The plus + exist in two forms: the binary form that we used above and the unary form.
The unary plus or, in other words, the plus operator + applied to a single value, doesn't do anything to numbers. But if the operand is not a number, the unary plus converts it into a number.
let x = 1;
alert(+x); // 1
let y = -2;
alert(+y); // -2
alert(+true); // 1
alert(+''); // 0
It actually does the same thing as Number(...), but is shorter.
Why are unary pluses applied to values before the binary ones? As we're going to see, that's because of their higher precedence.
Every operator has a corresponding precedence number. The one with the larger number executes first. If the precedence is the same, the execution order is from left to right.
Just note that unary operators are higher than corresponding binary ones.
unary operators > binary ones
= is also an operator. But assignment has very low priority.
Increasing or decreasing a number by one is among the most common numerical operations.
*Increment/decrement can only be applied to variables.
let counter = 0;
alert( ++counter); //1
let coutner = 0;
alert(counter++); // 0
*Increment/decrement among other operators
The operators ++/— can be used inside expressions as well. Their precedence is higher than most other arithmetical operations.
++/— 는 표현 안에서 또한 사용될 수 있다. 그들의 우선순위는 다른 산술 오퍼레이터 보다 높다.
let coutner = 1;
alert( 2 * ++counter); // 4
let counter = 1;
alert(2 * counter);
counter++;
같은 결과이지만, 밑의 형식으로 쓰는 것이 읽기에 쉽다.
The comma operator , is one of the rarest and most unusual operations. Sometimes, it's used to write shorter code.
The comma operator allows us to evaluate several expressions, dividing them with a comma , . Each of them is evaluated but only the result of the last one is returned.
여러가지 계산을 하게 해주는데 오직 마지막 계산의 결과만 리턴한다.
let a = (1 + 2, 3 + 4);
alert(a); // 7
comma operator has very low precedence, lower than =, so parentheses are important in the example below.