Front-end πŸ”₯πŸ–€/JavaScript

πŸ“š λ“œλ¦Όμ½”λ”© by μ—˜λ¦¬ JavaScript 정리 2. 데이터 νƒ€μž…

stable_sound 2022. 1. 21. 23:59

1. variable : λ³€μˆ˜

: 변경될 수 μžˆλŠ” κ°’

 

let : ES6μ—μ„œ μΆ”κ°€

let name = `ellie`;
console.log(name); // > ellie
name = `hello`;
console.log(name); // > hello

 

2. Block scope

let globalName = "global name"; 
//global λ³€μˆ˜λŠ” μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹œμž‘λΆ€ν„° 끝날 λ•ŒκΉŒμ§€ λ©”λͺ¨λ¦¬μ— 있기 λ•Œλ¬Έμ— μ΅œμ†Œν•œμœΌλ‘œ μ“°λŠ” 것이 μ’‹λ‹€.
{
  let name = "ellie";
  console.log(name);
  name = "hello";
  console.log(name);
}

console.log(globalName); // global λ³€μˆ˜μ΄κΈ° λ•Œλ¬Έμ— 좜λ ₯κ°€λŠ₯ > global name
console.log(name); // global λ³€μˆ˜κ°€ μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— λ³€μˆ˜λ₯Ό μ„ μ–Έν•œ λΈ”λ‘μ˜ λ‚΄λΆ€μ—μ„œλ§Œ 좜λ ₯ κ°€λŠ₯ > error

Block scope 이야기λ₯Ό ν•˜λ©΄ λΉ μ§€μ§€ μ•ŠλŠ” 것이 var 이닀.

 

이 varλŠ” ν˜„μž¬λŠ” μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ° μ΄μœ κ°€ μžˆλ‹€.

 

// varλŠ” μ“°μ§€λ§μž!
console.log(age); // > undefined
age = 4;
console.log(age); // > 4
var age;

// 심지어 varλŠ” 블둝 μŠ€μ½”ν”„λ„ μ—†λ‹€.

{
  age1 = 5;
  var age1;
}
console.log(age1); // 블둝 μ•ˆμ—μ„œ 선언해도 μ–΄λ””μ„œλ“  μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.

var의 경우 선언을 아직 ν•˜μ§€ μ•Šμ•˜λŠ”λ°λ„ μ½˜μ†”μ—μ„œ undefined둜 좜λ ₯이 κ°€λŠ₯ν•˜λ‹€.

 

λΆ„λͺ…νžˆ 아직 선언을 μ•ˆν–ˆλŠ”λ°.. 정말 μ΄μƒν•œ 녀석이닀. γ…‹γ…‹γ…‹γ…‹

 

이런 ν˜„μƒμ„ λ°”λ‘œ ν˜Έμ΄μŠ€νŒ…(λŒμ–΄μ˜¬λ¦¬λ‹€) μ΄λΌκ³  λΆ€λ₯Έλ‹€.

 

var hoisting : μ–΄λ””에 μ„ μ–Έν–ˆλŠ”μ§€μ— μƒκ΄€μ—†μ΄ μ œμΌ μœ„λ‘œ μ„ μ–Έμ„ λŒμ–΄ μ˜¬λ¦¬λŠ” κ²ƒ

 

그리고 varλŠ” 블둝 λ²”μœ„κ°€ μ—†κΈ° λ•Œλ¬Έμ— μ„ μ–Έλ§Œ ν•˜λ©΄ μ–΄λ””μ„œλ“  μ‚¬μš©ν•  수 μžˆλ‹€.

 

κ·Έλ ‡κΈ° λ•Œλ¬Έμ— varλŠ” μ‚¬μš©ν•˜μ§€λ§μž!

 

 

3. Constant

: ν•œλ²ˆ ν• λ‹Ήν•˜λ©΄ 변경이 λΆˆκ°€λŠ₯ν•œ κ°’

const max = 5;
max = 11; // > error

/* const λ₯Ό μ‚¬μš©ν•˜λŠ” 이유
 * λ³΄μ•ˆ : 해컀듀이 값을 계속 λ³€κ²½ν•  수 μžˆλŠ”λ° 이λ₯Ό λ°©μ§€ν•  수 μžˆλ‹€.
 * μŠ€λ ˆλ“œ μ•ˆμ „μ„± : ν”„λ‘œμ„ΈμŠ€ μ•ˆμ—μ„œ λ‹€μ–‘ν•œ μŠ€λ ˆλ“œκ°€ λ™μ‹œμ— μ‹€ν–‰λ˜λŠ”λ° λ™μ‹œμ— λ³€μˆ˜μ— μ ‘κ·Όν•΄μ„œ 값을 λ³€κ²½ν•  수 μžˆλŠ”λ° 이게 λΆˆκ°€λŠ₯ν•˜λ„λ‘ ν•΄μ•Όν•œλ‹€.
 * μΈκ°„μ˜ μ‹€μˆ˜ κ°μ†Œ
 */

 

4. Variable types 

// 4. 데이터 νƒ€μž…

// primitive, single item: number, string, boolean, null, undefined, symbol
// object, box container
// function, first-class function

//number
const count = 17;
const size = 17.1;
const infinity = 1 / 0;
const negativeInfinity = -1 / 0;
const nAn = `not a number` / 2;

console.log(`value: ${count}, type:${typeof count}`);
console.log(`value: ${size}, type:${typeof size}`);
console.log(`value: ${infinity}, type:${typeof infinity}`);
console.log(`value: ${negativeInfinity}, type:${typeof negativeInfinity}`);
console.log(`value: ${nAn}, type:${typeof nAn}`);

//string
const hello = "hello";
const greeting = hello + "name my is Lee";
console.log("value: " + hello + " type: " + typeof hello);
console.log(`value: ${hello}, type: ${typeof hello}`); //ν…œν”Œλ¦Ώ λ¦¬ν„°λŸ΄μ΄ 훨씬 νŽΈν•˜λ‹€.

//boolean (true/false)
const t = true;
const test = 3 < 1; // false
console.log(`value: ${t}, type: ${typeof t}`);
console.log(`value: ${test}, type: ${typeof test}`);

//null (μ—†λ‹€ ν……ν……λΉ„μ–΄μžˆλ‹€)
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);

//undefined (선언은 λ˜μ–΄μžˆμ§€λ§Œ 아무것도 값이 μ§€μ •λ˜μ–΄μžˆμ§€ μ•Šλ‹€)
let x = undefined;
console.log(`value: ${x}, type: ${typeof x}`);

//symbol
//κ°œμ²΄μ— λŒ€ν•œ 고유 μ‹λ³„μžλ₯Ό 생성
const symbol1 = Symbol("id");
const symbol2 = Symbol("id");
console.log(symbol1 === symbol2); // > false : λ™μΌν•œ string을 μž‘μ„±ν•΄λ„ λ‹€λ₯Έ symbol둜 λ§Œλ“€μ–΄μ§
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);

// object
const stable_sound = { name: "Lee", age: 24 };
// sound μžμ²΄λŠ” const 둜 μ •μ˜λ˜μ–΄μžˆκΈ° λ•Œλ¬Έμ— λ‹€λ₯Έ object둜 할당이 λΆˆκ°€λŠ₯.
// ν•˜μ§€λ§Œ κ·Έ μ•ˆμ— μžˆλŠ” nameκ³Ό age λ³€μˆ˜λŠ” λ‹€λ₯Έκ°’μœΌλ‘œ 할당이 κ°€λŠ₯ν•˜λ‹€.
console.log(stable_sound);
stable_sound.age = 23;
console.log(stable_sound);

 

5. Dynamic typing (동적 타이핑)

: λ³€μˆ˜ μ§€μ • μ‹œ, ν•΄λ‹Ή λ³€μˆ˜μ˜ 데이터 νƒ€μž…μ„ λͺ…μ‹œν•˜μ§€ μ•Šμ•„λ„ μžλ™μ μœΌλ‘œ νƒ€μž…μ„ ν•΄μ„ν•œλ‹€.

 

let text = `hello`;
console.log(text.charAt(0)); // > h
console.log(`value: ${text}, type: ${typeof text}`); // > type: string
text = 1;
console.log(`value: ${text}, type: ${typeof text}`); // > type: number
text = `7` + 5;
console.log(`value: ${text}, type: ${typeof text}`); // > type: string
text = `8` / `2`;
console.log(`value: ${text}, type: ${typeof text}`); // > type: number
// console.log(text.charAt(0)); // > error : Uncaught TypeError: text.charAt is not a function

// jsμ—μ„œλŠ” λŸ°νƒ€μž„μ‹œ νƒ€μž…μ΄ μ •ν•΄μ§€λŠ”λ° νƒ€μž…μ΄ 자꾸만 λ³€ν•˜κ²Œ λ˜λ‹ˆκΉŒ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.
// κ·Έλž˜μ„œ λ‚˜μ˜¨ 것이 typescript이닀.