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

๐Ÿ“• STUDY/JavaScript

[JavaScript] ๊ธฐ์ดˆ๋ฌธ๋ฒ•-2 (alert, prompt, confirm / ํ˜•๋ณ€ํ™˜)

[JavaScript] ๊ธฐ์ดˆ๋ฌธ๋ฒ•-2 (alert, prompt, confirm / ํ˜•๋ณ€ํ™˜)


โœ… ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋Šฅ (alert / prompt / comfirm)

โ–ถ alert : ์•Œ๋ ค์คŒ

โ–ถ prompt : ์ž…๋ ฅ ๋ฐ›์Œ

โ–ถ confirm : ํ™•์ธ ๋ฐ›์Œ

 
1. alert('message'); 
 
- ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ๊นŒ์ง€ ๊ณ„์†ํ•ด์„œ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด์—ฌ์คŒ
- ํ™•์ธ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ซํž˜
 
 
2. prompt('message', [default]);
 
- ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฉ”์„ธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
- ์ทจ์†Œ๋ฅผ ๋ˆ„๋ฅด๋ฉด null์„ ๋ฐ˜ํ™˜
 
const name = prompt("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.");

alert(`ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ${name}๋‹˜`);

 

- ๋‘๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๋„ฃ์œผ๋ฉด default ์ œ๊ณต ๊ฐ€๋Šฅ

 

let birthDay = prompt('์ƒ๋…„์›”์ผ์„ ์ž…๋ ฅํ•˜์„ธ์š”.', '2001-04-');
alert(`๋‹น์‹ ์˜ ์ƒ์ผ์ด ${birthDay} ๋งž์Šต๋‹ˆ๊นŒ?`);

 

 
 
3. confirm('message');
 
- ์‚ฌ์šฉ์ž์—๊ฒŒ ํ™•์ธ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ์šฉ๋„
- ํ™•์ธ : true ๋ฐ˜ํ™˜
- ์ทจ์†Œ : false ๋ฐ˜ํ™˜
let pushButton = confirm("๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
alert(pushButton);
// 'ํ™•์ธ' - true
// '์ทจ์†Œ' - false

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

 

1. ์ˆซ์žํ˜• ๋ณ€ํ™˜

Number('๋ฌธ์ž') : ๋ฌธ์ž โžก ์ˆซ์žํ˜•

let word = '500';

console.log(typeof(word)); // string
console.log(typeof(Number(word))); // number

 

- ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๊ฐ€ ์žˆ์œผ๋ฉด NaN ํƒ€์ž…์ด ๋œธ

console.log(
  Number(true),
  Number(false),
  Number("123"),
  Number("123rs")
  )
// 1 0 123 NaN

 

 

2. ๋ฌธ์žํ˜• ๋ณ€ํ™˜

String(์ˆซ์ž) : ์ˆซ์ž โžก ๋ฌธ์žํ˜• 

let num = 3;

console.log(
  typeof(num), // number
  typeof(String(num)), // string
  )

 

3. Boolean ํ˜•๋ณ€ํ™˜

Boolean(๋‚ด์šฉ) : ๋‚ด์šฉ โžก ๋ถˆ๋ฆฐํ˜•

- false : 0, '', null, undefined, NaN

- true : ๋‚˜๋จธ์ง€

console.log(
  Boolean(0), // false
  Boolean('0'), // true
  Boolean(''), // false
  Boolean(' ') // true
  )

 

 

4. ์˜ˆ์ œ

1) ์ž…๋ ฅ : prompt๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆ˜ํ•™๊ณผ ์˜์–ด ์ ์ˆ˜ ์ž…๋ ฅ

2) ์ถœ๋ ฅ : ์ˆ˜ํ•™๊ณผ ์˜์–ด ์ ์ˆ˜์˜ ํ‰๊ท ์„ ์ถœ๋ ฅ

 

4-1. [์˜ค๋ฅ˜ ๋ฐœ์ƒ] ์ถœ๋ ฅ ๊ฒฐ๊ณผ : 5045

const mathScore= prompt("์ˆ˜ํ•™ ๋ช‡์ ?"); // 100
const engScore= prompt("์˜์–ด ๋ช‡์ ?"); // 90
const result = (mathScore + engScore) /2;

console.log(result); // 5045 - ๋…ผ๋ฆฌ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

- ์˜ค๋ฅ˜ ์›์ธ : prompt๋กœ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์€ ๋ชจ๋‘ ๋ฌธ์ž์—ด

('100' + '90' = '10090')

>> 10090 / 2 = 5045๊ฐ€ ๋‚˜์˜จ ๊ฒƒ (๋ฌธ์ž์—ด์„ ์•Œ์•„์„œ ์ˆซ์žํ˜•์œผ๋กœ ๋ฐ”๊ฟ” ๋‚˜๋ˆ ์คŒ)

 

 

4-2. [๋ฏธํก] ์ˆซ์žํ˜• ๋ณ€์ˆ˜๋กœ ์ €์žฅ

const mathScore= 100;
const engScore= 90;
const result = (mathScore + engScore) /2;

console.log(result); // 95

 

- ์ฒ˜์Œ๋ถ€ํ„ฐ ์ˆซ์žํ˜• ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ

- ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด, ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ.

 

 

4-3. [์˜ค๋ฅ˜ ๋ฐœ์ƒ] ์ถœ๋ ฅ ๊ฒฐ๊ณผ : 5045

- Number('๋ฌธ์ž') ์‚ฌ์šฉ : ๋ฌธ์ž๋ฅผ ์ˆซ์žํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์คŒ

const mathScore= prompt("์ˆ˜ํ•™ ๋ช‡์ ?");
const engScore= prompt("์˜์–ด ๋ช‡์ ?");
const result = Number(mathScore + engScore) /2;

console.log(result); // 5045

 

- ์˜ค๋ฅ˜ ์›์ธ : 1๋ฒˆ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜

>> '50' + '45' = 5045๋ฅผ ์ˆซ์žํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

4-4. [ํ•ด๊ฒฐ]

- Number('๋ฌธ์ž') ์‚ฌ์šฉ : ๋ฌธ์ž๋ฅผ ์ˆซ์žํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์คŒ

const mathScore= prompt("์ˆ˜ํ•™ ๋ช‡์ ?");
const engScore= prompt("์˜์–ด ๋ช‡์ ?");
const result = (Number(mathScore) + Number(engScore)) /2;

console.log(result);

๐Ÿ“› ์ฃผ์˜ ๐Ÿ“›
Number(null) // 0 - prompt์—์„œ ์ทจ์†Œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ null ๋ฐ˜ํ™˜
Number(undefined) // NaN

Boolean(0) - false

Boolean('0') - true
Boolean('') - false // ์•„๋ฌด๊ฒƒ๋„ ์—†์Œ
Boolean(' ') - true // ๊ณต๋ฐฑ์ด ํฌํ•จ๋  ๊ฒฝ์šฐ