단항 연산자 (increment, ++)
i = i + 1
++위치에 따라 값이 달라진다.
- ++i : 증가후 다른연산 진행
let foo = { bar: 0 } let count = 0; if (true) count = ++foo.bar; console.log(foo.bar) // 1 console.log(count) // 1 // foo.bar에 ++연산 먼저 해준 후, // count에 할당해서 각각 1이 나온다.
- i++ : 다른 연산 진행후 증가
let foo = { bar: 0 } let count = 0; if (true) count = foo.bar++; console.log(foo.bar) // 1 console.log(count) // 0 // count에는 0인 상태의 foo.bar를 할당한 후(다른연산), // ++연산자로 foo.bar에 1을 증감했다.
i + 1
현재 i값에 1을 더해 쓴다는 의미 > i값 증가 Xlet foo = { bar: 0 } let count = 0; if (true) count = foo.bar + 1; console.log(foo.bar) // 0 console.log(count) // 1
설명: