๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/JavaScript

[๋“œ๋ฆผ์ฝ”๋”ฉ by ์—˜๋ฆฌ] JavaScript ๊ธฐ์ดˆ ๊ฐ•์˜(2) (ES5+)

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 10. 6.
๋ฐ˜์‘ํ˜•

'use strict' ์‚ฌ์šฉ

'use strict';

 

 

Variable (๋ณ€์ˆ˜)

- ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋Š” ๊ฐ’

- JavaScript์—์„œ๋Š” let ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. (ES6์—์„œ ์ถ”๊ฐ€๋จ)

 

๐Ÿ”ฅ let ์ด์ „์—๋Š” var์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ var์˜ ์‚ฌ์šฉ์€ ์ง€์–‘ํ•œ๋‹ค!

var์€ Block Scope๋„ ๋ฌด์‹œํ•œ๋‹ค.

 

 

 

Block Scope

{
  let name = 'yunkyung';
  console.log(name);  // yunkyung
  name = 'hello';
  console.log(name);  // hello
}

console.log(name);    // ์ถœ๋ ฅ ์•ˆ๋จ

์ด๋ ‡๊ฒŒ block ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด block ๋ฐ–์—์„œ๋Š” ์•ˆ์˜ ๋‚ด์šฉ๋“ค์„ ๋ณผ ์ˆ˜ ์—†๋‹ค.

 

 

 

Constants

- ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์ž ๊ฒจ ์žˆ๋‹ค.

- ์„ ์–ธ๊ณผ ๋™์‹œ์— ํ• ๋‹น์ด ๋˜๋ฉด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

- let (mutable) vs const (immutable)

- ๋ณด์•ˆ, ์‹ค์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

 

 

Variable types

์–ด๋–ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋“  primitive type๊ณผ Object type ๋‘๊ฐ€์ง€๋กœ ๋‚˜๋‰˜์–ด์ง„๋‹ค.

 

  • Primitive type

๋”์ด์ƒ ์ž‘์€ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ ์งˆ ์ˆ˜ ์—†๋Š” ํ•œ ๊ฐ€์ง€์˜ ์•„์ดํ…œ.

number, string, boolean, null, undefined

 

-Number

// number
const count = 17;  // Integer
const size = 17.1;  // decimal number
console.log(`value: ${count}, type: ${typeof count}`);
console.log(`value: ${size}, type: ${typeof size}`);

ํƒ€์ž… ์ƒ๊ด€์—†์ด ์ „๋ถ€ number ๋กœ ๋‚˜์˜จ๋‹ค.

 

 

// number - speicla numeric values: infinity, -infinity, NaN
const infinity = 1 / 0;
const negativeInfinity = -1 / 0;
const nAn = 'not a number' / 2;
console.log(infinity);          // infinity
console.log(negativeInfinity);  // -infinity
console.log(nAn);               // NaN

NaN: not a number

 

์ˆซ์ž์˜ ๋งจ ๋งˆ์ง€๋ง‰์— 'n'๋งŒ ๋ถ™์ด๋ฉด bigInt๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

(์ตœ๊ทผ์— ์ถ”๊ฐ€๋œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํฌ๋กฌ, ํŒŒ์ด์–ดํญ์Šค์—์„œ๋งŒ ์ง€์›ํ•œ๋‹ค)

// bigInt (fairly new, don't use it yet)
const bigInt = 1234567890123456789012345678901234567890n; // over (-2**53) ~ 2*53)
console.log(`value: ${bigInt}, type: ${typeof bigInt}`);

 

 

-String

  • ๋‹ค๋ฅธ string๊ณผ + ๋ฅผ ์ด์šฉํ•ด์„œ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐฑํ‹ฑ(`)์œผ๋กœ ๊ฐ์‹ธ์„œ ๋ณ€์ˆ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
// string
const char = 'c';
const brendan = 'brendan';
const greeting = 'hello ' + brendan;
console.log(`value: ${greeting}, type: ${typeof greeting}`);
const helloBob = `hi ${brendan}!`;  //template literals (string)
console.log(`value: ${helloBob}, type: ${typeof helloBob}`);
console.log('value: ' + helloBob + ' type: ' + typeof helloBob);

ํƒ€์ž…์€ ๋‹ค string์œผ๋กœ ๋‚˜์˜จ๋‹ค.

 

// boolean
// false: 0, null, undefined, NaN, ''
// true: any other value
const canRead = true;
const test = 3 < 1; // false
console.log(`value: ${canRead}, type: ${typeof canRead}`);
console.log(`value: ${test}, type: ${typeof test}`);

 

 

// null
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);

// undefined
let x;
console.log(`value: ${x}, type: ${typeof x}`);

 

 

-Symbol

  • ๊ณ ์œ ํ•œ ์‹๋ณ„์ž
  • ๋™์‹œ ๋‹ค๋ฐœ์ ์œผ๋กœ ์ผ์–ด๋‚  ์ฝ”๋“œ์—์„œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋™์ผํ•œ string์œผ๋กœ ์ž‘์„ฑํ•˜์—ฌ๋„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.
  • for ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋™์ผํ•œ symbol์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ์ถœ๋ ฅ์‹œ description ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
// symbol, create unique identifiers for objects
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2);
const gSymbol1 = Symbol.for('id');
const gSymbol2 = Symbol.for('id');
console.log(gSymbol1 === gSymbol2); 
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);

 

 

 

  • Object type

-Object

์‹ฑ๊ธ€ ์•„์ดํ…œ๋“ค์„ ๋ฌถ์–ด์„œ ํ•œ ๋ฐ•์Šค({})๋กœ ๊ด€๋ฆฌ. ํ•จ์ˆ˜์— ์ธ์ž๋กœ ์ „๋‹ฌ ๊ฐ€๋Šฅ. ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ function ๋ฆฌํ„ด ๊ฐ€๋Šฅ.

function, first-class-function ๋ณ€์ˆ˜์— ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

// object, real-life object, data structure
const ellie = { name: 'ellie', age: 20 };
ellie.age = 21;
  • ํ•œ ๋ฒˆ ํ• ๋‹น๋œ object๋Š” ๋‹ค๋ฅธ object๋กœ ํ• ๋‹น์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. (const๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ์ธํ„ฐ๊ฐ€ ์ž ๊ฒจ์žˆ๊ธฐ ๋•Œ๋ฌธ!)
  • object ์•ˆ์˜ ๋ณ€์ˆ˜์˜ ๊ฐ’(name, age)์€ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

  • Dynamic Typing

JavaScript๋Š” ์„ ์–ธ์‹œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ช…์‹œํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋Ÿฐํƒ€์ž„์—์„œ, ํ• ๋‹น๋œ ๊ฐ’์— ๋”ฐ๋ผ ํƒ€์ž…์ด ๋ณ€๊ฒฝ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ๋‹ค์ˆ˜์˜ ์—”์ง€๋‹ˆ์–ด, ๊ทœ๋ชจ๊ฐ€ ์žˆ๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ์˜ ์›์ธ์ด ๋  ์ˆ˜ ์žˆ์Œ -> TypeScript๋กœ ๋ณด์™„

// 5. Dynamic typing: dynamically typed language
let text = 'hello';
console.log(text.charAt(0));  // h
console.log(`value: ${text}, type: ${typeof text}`);
text = 1;
console.log(`value: ${text}, type: ${typeof text}`);
text = '7' + 5;
console.log(`value: ${text}, type: ${typeof text}`);
text = '8' / '2';
console.log(`value: ${text}, type: ${typeof text}`);
console.log(text.charAt(0));

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€