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

๐Ÿ“• STUDY/JavaScript

[JavaScript] ๊ธฐ์ดˆ๋ฌธ๋ฒ•-1 (๋ณ€์ˆ˜/ ์ž๋ฃŒํ˜•)

JavaScript ๊ธฐ์ดˆ๋ฌธ๋ฒ•-1 (๋ณ€์ˆ˜ / ์ž๋ฃŒํ˜•)


โœ… ๋ณ€์ˆ˜

 

โ–ถ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹ (var / let / const)

var msg = 'hi';
let name = "hyunjin";
const message = `${msg}, My name is ${name}`;

console.log(message); // hi, My name is hyunjin

 

1. var ๋ณ€์ˆ˜๋ช… = ๋ณ€์ˆ˜์˜ ๋‚ด์šฉ

- ๋ณ€์ˆ˜์˜ ์žฌ์„ ์–ธ ๊ฐ€๋Šฅ

- ๋ณ€์ˆ˜์˜ ์žฌํ• ๋‹น ๊ฐ€๋Šฅ

- ์žฅ์  : ๋ณ€์ˆ˜ ์žฌ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ์—์„œ๋Š” ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

- ๋‹จ์  : ๋ณ€์ˆ˜ ์žฌ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ์ฝ”๋“œ๋Ÿ‰์ด ๋งŽ์•„์ง€๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ์˜ ์›์ธ์ด ๋จ.

var msg = 'hi';
console.log(msg); // hi

var msg = 'hello'; // ์žฌ์„ ์–ธ ๊ฐ€๋Šฅ
console.log(msg); // hello

 

2. let ๋ณ€์ˆ˜๋ช… = ๋ณ€์ˆ˜์˜ ๋‚ด์šฉ

- ๋ณ€์ˆ˜์˜ ์žฌ์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ

- ๋ณ€์ˆ˜์˜ ์žฌํ• ๋‹น ๊ฐ€๋Šฅ

let age = 20;
// let age = 22; - ์žฌ์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ ์˜ค๋ฅ˜
age = 22; // ์žฌํ• ๋‹น ๊ฐ€๋Šฅ

console.log(age);

 

- ์„ ์–ธ ํ›„ ์•„๋ฌด๊ฒƒ๋„ ํ• ๋‹นํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด? undefined 

- ์„ ์–ธ ํ›„ null์„ ํ• ๋‹นํ–ˆ๋‹ค๋ฉด? null

let age;
console.log(age); // undefined ์ถœ๋ ฅ

let age2 = null;
console.log(age2); // null ์ถœ๋ ฅโ€‹

 

3. const ๋ณ€์ˆ˜๋ช… = ๋ณ€์ˆ˜์˜ ๋‚ด์šฉ

- ๋ณ€์ˆ˜์˜ ์žฌ์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ

- ๋ณ€์ˆ˜์˜ ์žฌํ• ๋‹น ๋ถˆ๊ฐ€๋Šฅ

const age = 22;
// age = 23; - ์žฌํ• ๋‹น ๋ถˆ๊ฐ€๋Šฅ ์˜ค๋ฅ˜
// const age = 23; - ์žฌ์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ ์˜ค๋ฅ˜

console.log(age); // 22

โœ… ์ž๋ฃŒํ˜•

 

โ–ถ ๋‹ค์–‘ํ•œ ์ž๋ฃŒํ˜• (์ˆซ์žํ˜•, ๋ฌธ์ž์—ด...)

 

1. NaN

- Not-A-Number ์˜ ์ค„์ž„๋ง

- ์ˆซ์žํ˜•์ด ์•„๋‹ˆ๋ผ๋Š” ๋œป

const name = "Mike";
const y = name/2
const x = 5;

console.log(x/2); // 2.5
console.log(y); // NaN - ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋‹ค ๋ผ๋Š” ๋œปโ€‹

 

2. ๋ฌธ์ž์—ด ๋ง์…ˆ

๋ฌธ์ž์—ด ์‚ฌ์ด์— ์ˆซ์ž๋ฅผ ๋”ํ•˜๋ฉด ์ˆซ์ž๋„ ๋ฌธ์žํ˜•์œผ๋กœ ๋ณ€ํ™˜๋จ

const a = "๋‚˜๋Š” ";
const b = 22; // ์ด๋•Œ ์ˆซ์ž b๋Š” ๋ฌธ์žํ˜•์œผ๋กœ ๋ณ€ํ™˜๋จ.
const c = "์‚ด ์ž…๋‹ˆ๋‹ค.";

console.log(a + b + c); // ๋‚˜๋Š” 22์‚ด์ž…๋‹ˆ๋‹ค.