๐Ÿ“ TIL) operator (์—ฐ์‚ฐ์ž)

Solmiiยท2020๋…„ 4์›” 25์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
4/24
post-thumbnail

๐Ÿ“ Operator

String concatenation (๋ฌธ์ž์—ด ์—ฐ๊ฒฐ)

console.log("my" + "cat");
console.log("1" + 2); //์ˆซ์ž๊ฐ€ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋˜์–ด ์ถœ๋ ฅ๋œ๋‹ค.
console.log(`string literals; 1 + 2 = ${1 + 2}`);  //์ค‘๊ฐ„์— ์ค„์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜, ํŠน์ˆ˜ ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด๋„ ๋ฌธ์ž์—ด๋กœ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ

๋ฌธ์ž๋ฅผ + ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด์„œ ์—ฐ์†์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.


Arithmetic Operators (์‚ฐ์ˆ  ์—ฐ์‚ฐ)

console.log(1 + 1); //add
console.log(1 - 1); //subtract
console.log(1 / 1); //divide
console.log(1 * 1); //multiply
console.log(1 % 1); //remainder (๋‚˜๋จธ์ง€)
console.log(1 ** 1); //exponentiation (๊ฑฐ๋“ญ์ œ๊ณฑ)

๋ง‰๊ฐ„! ํผ์„ผํŠธ(%) ๊ตฌํ•˜๊ธฐ ๊ณต์‹!!

์ „์ฒด๊ฐ’(x)์—์„œ ์ผ๋ถ€๊ฐ’(y)์€ ๋ช‡ ํผ์„ผํŠธ?

y / x * 100

ex) 20 / 55 * 100 // 36.363636......

์ „์ฒด๊ฐ’(x)์—์„œ ๋ช‡ ํผ์„ผํŠธ๋Š” ์–ผ๋งˆ?

x * ํผ์„ผํŠธ / 100

ex) 55 * 20 / 100 // 11

๋ง‰๊ฐ„! ๋ถ€๋™ ์†Œ์ˆ˜์  (Floating Point) vs ๊ณ ์ • ์†Œ์ˆ˜์  (Fixed Point)
๋ธŒ๋ผ์šฐ์ € ๊ฐœ๋ฐœ์ž ๋„๊ตฌ-์ฝ˜์†”์—์„œ ํ•ด๋‹น๊ฐ’์„ ์ž…๋ ฅํ•ด๋ณด์ž!

0.1 + 0.2;

๋‹น์—ฐํžˆ 0.3์ด ๋‚˜์˜ค๊ฒ ์ง€? ํ•˜๊ณ  Enter๋ฅผ ๋ˆŒ๋Ÿฌ๋ณด๋ฉด...๋ ์šฉ~
โ‡’ ์ปดํ“จํ„ฐ๋Š” ์†Œ์ˆ˜๋ฅผ 2์ง„์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์œ„์˜0.1 + 0.2; ๊ฐ™์€ 10์ง„์ˆ˜ ์†Œ์ˆ˜๋ฅผ ์ •ํ™•ํžˆ ๋‹ค๋ฃฐ ์ˆ˜ ์—†๋‹ค.

์šฐ๋ฆฌ๋Š” 0.1 ์ด๋ผ๊ณ  ์ž…๋ ฅํ–ˆ์ง€๋งŒ, ์ปดํ“จํ„ฐ ๋‚ด๋ถ€์—์„œ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ›์•„๋“ค์ด๋Š” ๊ณผ์ •์—์„œ ์•ฝ๊ฐ„์˜ ์˜ค์ฐจ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์ด๋ฅผ ๋ฐ˜์˜ฌ๋ฆผ ์˜ค์ฐจ(rounding error)๋ผ๊ณ  ํ•œ๋‹ค.

โ‡’ ์™œ ์ด๋Ÿฐ ์˜ค์ฐจ๋ฅผ ๊ทธ๋ƒฅ ๋‘๋Š”๊ฑธ๊นŒ?
์ปดํ“จํ„ฐ์˜ ์ €์žฅ์šฉ๋Ÿ‰์€ ํ•œ์ •๋˜์–ด ์žˆ๊ณ  0๊ณผ 1๋ฐ–์— ๋‹ค๋ฃจ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์—, 10์ง„์ˆ˜ ์†Œ์ˆ˜๋ฅผ ์•„์ฃผ ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด. ์ฆ‰, ๊ณ„์‚ฐ ์ƒ์˜ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ์ด๋ ‡๊ฒŒ ์„ค๊ณ„ํ•œ ๊ฒƒ!

(p.s - ๊ธˆ์œต ๋ถ„์•ผ์ฒ˜๋Ÿผ ์กฐ๊ธˆ์˜ ์˜ค์ฐจ๋„ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๋ถ„์•ผ์—์„œ๋Š” ์ •ํ™•ํ•œ ์—ฐ์‚ฐ์„ ์œ„ํ•œ ์ „์šฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.)


Increment and Decrement Operators (์ฆ๊ฐ€/๊ฐ์†Œ ์—ฐ์‚ฐ)

pre increment/decrement

let counter = 2;
const preIncrement = ++counter;

๋ณ€์ˆ˜ ์•ž์— ++๊ธฐํ˜ธ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด pre Increment๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค.

counter = counter + 1; //counter์˜ ๊ฐ’์„ 1 ์ฆ๊ฐ€ํ•œ ํ›„์—
preIncrement = counter; //preIncrement ๋ณ€์ˆ˜์— counter์˜ ๊ฐ’์„ ํ• ๋‹น

pre Increment ๋Š” ์œ„์˜ ์ฝ”๋“œ์™€ ๋™์ผํ•œ ์—ญํ• ์„ ํ•œ๋‹ค!

const preDecrement = --counter;

๋งˆ์ด๋„ˆ์Šค - ์—ญํ• ์„ ํ•˜๋Š” pre Decrement ๋„ ๊ฐ™์€ ๋ฐฉ์‹!

post incement/decrement

let counter = 2;
const postIncrement = counter++;

๋ณ€์ˆ˜ ๋’ค์— ++๊ธฐํ˜ธ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด post Increment๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค.

preIncrement = counter; //postIncrement ๋ณ€์ˆ˜์— counter์˜ ๊ฐ’์„ ํ• ๋‹นํ•œ ํ›„์—
counter = counter + 1; //counter์˜ ๊ฐ’์„ 1 ์ฆ๊ฐ€

post Increment ๋Š” ์œ„์˜ ์ฝ”๋“œ์™€ ๋™์ผํ•œ ์—ญํ• ์„ ํ•œ๋‹ค!

const postDecrement = counter--;

๋งˆ์ด๋„ˆ์Šค - ์—ญํ• ์„ ํ•˜๋Š” post Decrement ๋„ ๊ฐ™์€ ๋ฐฉ์‹!


Assignment Operators (ํ• ๋‹น ์—ฐ์‚ฐ)

let x = 3;
let y = 6;

x += y; // 9 (x = x + y; (x๊ฐ’์„ x + y๋กœ ์ˆ˜์ •))
x -= y; // 3 (x = x - y;)
x *= y; // 18 (x = x * y;)
x /= y; // 3 (x = x / y;)

x += y; โ‡’ x = x + y;๋ฅผ ๋œปํ•œ๋‹ค.


Comparison Operators (๋น„๊ต ์—ฐ์‚ฐ)

console.log(10 < 6); // less than
console.log(10 <= 6); // less than or equal
console.log(10 > 6); // greater than
console.log(10 >= 6); // greater than or equal

Logical Operators (๋…ผ๋ฆฌ ์—ฐ์‚ฐ)

const value1 = false;
const value2 = 4 < 2; //4 < 2๋Š” ํ‹€๋ฆฐ ๊ฐ’์œผ๋ฏ€๋กœ, ๊ฐ’์€ flase

|| (or)

console.log(`or: ${value1 || value2 || check()}`);
function check() {
   for{
   ๋Œ€์ถฉ ์—„์ฒญ ๋ณต์žกํ•œ ํ•จ์ˆ˜
   }
   return true; //์•„๋ฌดํŠผ ๊ฒฐ๊ตญ์€ true๋ฅผ return ํ•˜๋Š” ์•„์ด!
}
  1. or โ‡’ ์ž…๋ ฅํ•œ ๊ฐ’ 3๊ฐœ ์ค‘์— ํ•˜๋‚˜๋ผ๋„ true ๋ฉด true ๊ฐ€ ์ถœ๋ ฅ
  2. value1 = false... โ‡’ value2 = flase... โ‡’ check() = true!!
  3. ๊ฒฐ๊ตญ์€ true๊ฐ€ ์ถœ๋ ฅ!!!

&& (and)

console.log(`or: ${value1 && value2 && check()}`);
function check() {
   for{
   ๋Œ€์ถฉ ์—„์ฒญ ๋ณต์žกํ•œ ํ•จ์ˆ˜
   }
   return true; //์•„๋ฌดํŠผ ๊ฒฐ๊ตญ์€ true๋ฅผ return ํ•˜๋Š” ์•„์ด!
}
  1. and โ‡’ ์ž…๋ ฅํ•œ ๊ฐ’ 3๊ฐœ ์ค‘์— ํ•˜๋‚˜๋ผ๋„ false ๋ฉด false ๊ฐ€ ์ถœ๋ ฅ
  2. value1 = false!! ๋!!
  3. ๋’ค์—๊นŒ์ง€ ์—ฐ์‚ฐํ•  ํ•„์š” ์—†์ด ๋ฐ”๋กœ false๊ฐ€ ์ถœ๋ ฅ!!!

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๊ฐ€ ์ค‘๋ณต๋  ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์„๋ ๊นŒ?

if (age > 65 || age < 21 && res === "ํ•œ๊ตญ")

  • age๊ฐ€ 65๋ฅผ ๋„˜๊ฑฐ๋‚˜ ํ˜น์€
  • age๊ฐ€ 21๋ณด๋‹ค ์•„๋ž˜์ด๋ฉด์„œ ๊ทธ๋ฆฌ๊ณ  ์‚ฌ๋Š” ๊ณณ์€ ํ•œ๊ตญ์ด๋‹ค.
  • age๊ฐ€ 65๋ฅผ ๋„˜๊ฑฐ๋‚˜ ํ˜น์€ age๊ฐ€ 21๋ณด๋‹ค ์•„๋ž˜์ด๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์‚ฌ๋Š” ๊ณณ์€ ํ•œ๊ตญ์ด๋‹ค.

๐Ÿคท๐Ÿปโ€โ™€๏ธ ๋‘˜ ๋‹ค๋กœ ํ•ด์„๋  ์ˆ˜ ์žˆ๋Š”๋ฐ... ์ปดํ“จํ„ฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์„ํ• ๊นŒ?
โ‡’ ์ปดํ“จํ„ฐ๋Š” ์™ผ์ชฝ์—์„œ ๋ถ€ํ„ฐ ํ•ด์„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„๊ฐ€ ๋งž๋Š” ํ•ด์„์ด๋‹ค!

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿซ ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์ค„ ๊ฒƒ!!

1๋ฒˆ์œผ๋กœ ํ•ด์„ํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด :
if (age > 65 || (age < 21 && res === "ํ•œ๊ตญ"))

2๋ฒˆ์œผ๋กœ ํ•ด์„ํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด :
if ((age > 65 || age < 21) && res === "ํ•œ๊ตญ")

๐Ÿ“Œ ์ค‘์š” point!!!!

or์—ฐ์‚ฐ์ž๋Š” ์ˆœ์ฐจ์ ์œผ๋กœ ์—ฐ์‚ฐํ•˜๋‹ค๊ฐ€ ํ•˜๋‚˜๋ผ๋„ true๊ฐ€ ๋‚˜์˜ค๋ฉด ์—ฐ์‚ฐ์„ ๋ฉˆ์ถ”๊ณ  ๋ฐ”๋กœ true๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค!
(and๋Š” ํ•˜๋‚˜๋ผ๋„ false๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ฐ”๋กœ false ์ถœ๋ ฅ)

EX) or ์—ฐ์‚ฐ์—์„œ ๋งŒ์•ฝ const value1 = true; ๋ผ๋ฉด, ๋’ค์—๊นŒ์ง€ ๊ฐˆ ํ•„์š”๋„ ์—†์ด ๋ฐ”๋กœ true๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋๋‚ด๋ฒ„๋ฆผ!

โ‡’ ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์—ฐ์‚ฐ์ด ๋งŽ๊ณ  ๋ณต์žกํ•œ ํ•จ์ˆ˜๋‚˜, extension ๊ฐ™์€๊ฑธ ์•ž์— ๋‘๊ฒŒ๋˜๋ฉด ๊ณ„์‚ฐํ•˜๋Š๋ผ ์‹œ๊ฐ„ ๋‹ค ๊ฐ„๋‹น..... ์‹ฌํ”Œํ•œ value ๊ฐ’์„ ์ œ์ผ ์•ž์— ๋‘๊ณ , ๋ณต์žกํ•œ ํ•จ์ˆ˜ ๋“ฑ์€ ์ œ์ผ ๋’ค์— ๋‘๋Š”๊ฒŒ ์ข‹๋‹ค!!!

๐Ÿ“Œ or๊ณผ and๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ„ํŽธํ•ด์„œ null check๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค!

๐Ÿ’๐Ÿปโ€โ™€๏ธ null check๋ž€?
null์ด๋‚˜ undefined ์€ ์–ด๋–ค ๋ณ€์ˆ˜์—๋„, ์–ด๋–ค ์†์„ฑ์—๋„ ๋“ค์–ด์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ์งค ๋•Œ๋Š” ํ•ญ์ƒ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์—†๋Š” ๊ฒฝ์šฐ (์ฆ‰ null ํ˜น์€ undefined์ธ ๊ฒฝ์šฐ)๋ฅผ ๋ชจ๋‘ ๊ณ ๋ คํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์งœ์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ ์–ด๋–ค ๊ฐ’์ด null ํ˜น์€ undefined์ธ์ง€ ํ™•์ธํ•˜๋Š” ์ž‘์—…์„ null check ๋ผ๊ณ  ํ•œ๋‹ค!

function printIfNotNull(input) {
  if (input !== null **&&** input !== undefined) {
    console.log(input);
  }
}

๋งค๋ฒˆ ์ด๋ ‡๊ฒŒ ๊ธธ๊ฒŒ ์ž‘์„ฑํ•  ํ•„์š”๋Š” ์—†๊ณ , ๋ณดํ†ต์€ input !== null && input !== undefined; ๋“ฑ์œผ๋กœ ์ค„์—ฌ์„œ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.
(null๊ณผ undefined์€ ๊ณต๋ถ€ํ•˜๋‹ค ๋ณด๋‹ˆ ์ƒ๊ฐ๋ณด๋‹ค ๋‚ด์šฉ์ด ๊ธธ์–ด์„œ ๋‹ค์Œ์— ๋”ฐ๋กœ ์ •๋ฆฌํ•ด์„œ ์˜ฌ๋ฆฌ๋Š”๊ฑธ๋กœ!!! ์ ˆ๋Œ€ ๊ท€์ฐฎ์€๊ฑฐ ์•„๋‹˜)

! (not)

const value1 = false;
console.log(!value1); //value์˜ ๊ฐ’์ด true๊ฐ€ ๋œ๋‹ค.

๊ฐ’์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.


Equality operator (๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ)

const stringFive = "5";
const numberFive = 5;

console.log(stringFive == numberFive); //true
console.log(stringFive != numberFive); //false

console.log(stringFive === numberFive); //false
console.log(stringFive !== numberFive); //true

== (abstract equality)

ํ”ผ์—ฐ์‚ฐ์ž์˜ type์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ, type์„ ๋ณ€ํ™˜ํ•œ ํ›„ ๋น„๊ต

stringFive์™€ numberFive ๋Š” type์€ ๋‹ค๋ฅด์ง€๋งŒ, type์„ ๋ฌด์‹œํ•˜๊ณ  ํ™•์ธํ•˜๋ฉด ๊ฐ’์€ ๊ฐ™๊ธฐ๋•Œ๋ฌธ์— == ๋กœ ๊ฒ€์‚ฌ์‹œ true๊ฐ€ ๋‚˜์˜จ๋‹ค.

null check ํ•  ๋•Œ == , != ๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

=== (strict equality)

ํ”ผ์—ฐ์‚ฐ์ž์˜ type์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ ๋ฌด์กฐ๊ฑด false๋ฅผ ๋ฐ˜ํ™˜

stringFive์™€ numberFive ๋Š” type์„ ๋ฌด์‹œํ•˜๋ฉด ๊ฐ™์€ ๊ฐ’์ด์ง€๋งŒ, type์€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— === ๋กœ ๊ฒ€์‚ฌ์‹œ false๊ฐ€ ๋‚˜์˜จ๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ค‘์— Equality๋กœ ๊ฒ€์‚ฌํ•  ์ผ์ด ์ƒ๊ธฐ๋ฉด === ์‚ฌ์šฉํ•˜๋Š”๊ฑธ ๊ถŒ์žฅํ•œ๋‹ค! (null check ์ œ์™ธ)

Object.is

๋‘ ์ธ์ˆ˜๊ฐ€ ์ •๋ง๋กœ ๊ฐ™์€ ๊ฐ’์ธ์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.

์•„๋ž˜์˜ ๋‘ ์˜ˆ์™ธ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” === ์™€ ๋˜‘๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.

Object.is(NaN, NaN); // true (=== ์—์„œ๋Š” false)
Object.is(0, -0); // false (=== ์—์„œ๋Š” true)

Object Equality by reference

const solmi1 = {name: "solmi"};
const solmi2 = {name: "solmi"};
const solmi3 = solmi1;

์ด 3๊ฐœ์˜ object๋Š” ๋˜‘๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ์ง€๋งŒ, ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ์—๋Š” solmi1, solmi2๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ reference์— ์ €์žฅ๋˜์–ด ์žˆ๊ณ , solmi3์€ solmi1๊ณผ ๊ฐ™์€ reference์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ƒํƒœ๋‹ค!

์—ฌ๊ธฐ์„œ Equality๋กœ ๊ฒ€์‚ฌํ•ด๋ณด๋ฉด ์–ด๋–ค ๊ฐ’์ด ์ถœ๋ ฅ๋ ๊นŒ?

console.log(solmi1 == solmi2); //false
console.log(solmi1 === solmi2); //false
console.log(solmi1 === solmi3); //true

solmi1๊ณผ solmi2๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ reference์— ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, === ๋Š” ์ปค๋…• == ๋„ flase!

ํ•˜์ง€๋งŒ solmi1๊ณผ solmi3์€ ๊ฐ™์€ reference์— ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, true!

profile
ํ•˜๋ฃจ๋Š” ์น˜์—ดํ•˜๊ฒŒ ์ธ์ƒ์€ ์—ฌ์œ ๋กญ๊ฒŒ

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2020๋…„ 4์›” 26์ผ

์กฐ๊ธˆ ๋ฐ€๋ ค๋„ ๊ดœ์ฐฎ์ง€์š” !

1๊ฐœ์˜ ๋‹ต๊ธ€