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

[JavaScript] ๊ธฐ์ดˆ ๋ฌธ๋ฒ•, ๋ณ€์ˆ˜, ์—ฐ์‚ฐ์ž

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

<๊ฐœ๋ฐœ ํ™˜๊ฒฝ>

์ค€๋น„๋ฌผ : ๋น„์ฃผ์–ผ ์ŠคํŠœ๋””์˜ค ์ฝ”๋“œ, ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €

(๊ทธ ๋ฐ–์— Edit Plus, Notepad++, Atom, Sublime Text, Brackets, Webstorm ๋“ฑ์ด ์žˆ์ง€๋งŒ

์šฐ๋ฆฌ๋Š” ๋น„์ฃผ์–ผ ์ŠคํŠœ๋””์˜ค ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.)

โ€‹

 

โ–ถ ๊ฐ„๋‹จํ•œ ์ถœ๋ ฅ๋ฌธ ๋งŒ๋“ค๊ธฐ

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<h1>Welcome My World</h1>
</body>
</html>

 

 

โ–ถ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๊ฐ„๋‹จํ•œ ์‹คํ–‰๋ฌธ ์ž‘์„ฑํ•˜๊ธฐ

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>๋ง›๋ณด๊ธฐ ์˜ˆ์ œ</title>
</head>
<body>
	<script>
		var age = prompt("๋‹น์‹ ์˜ ๋‚˜์ด๋Š”?", "0");
		if(age >= 20) {
			document.write("๋‹น์‹ ์€ ์„ฑ์ธ์ž…๋‹ˆ๋‹ค.");
		} else {
			document.write("๋‹น์‹ ์€ ๋ฏธ์„ฑ๋…„์ž์ž…๋‹ˆ๋‹ค.");
		}
	</script>
</body>
</html>

 

 

 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ ๋ฌธ๋ฒ•

โ–ถ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์„ ์–ธ๋ฌธ

์„ ์–ธ๋ฌธ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์˜์—ญ์„ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. <script>๋ผ๋Š” ํƒœ๊ทธ๋กœ ์„ ์–ธ๋ฌธ์ด ์‹œ์ž‘๋œ ๊ณณ๋ถ€ํ„ฐ </script>๋ผ๋Š” ํƒœ๊ทธ๋กœ ์ข…๋ฃŒ๋œ ๊ณณ๊นŒ์ง€๋ฅผ ์Šคํฌ๋ฆฝํŠธ ์˜์—ญ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์„ ์–ธ๋ฌธ์€ <head> ํƒœ๊ทธ ์˜์—ญ ๋˜๋Š” <body> ํƒœ๊ทธ ์˜์—ญ์— ์„ ์–ธํ•˜๋ฉด ๋œ๋‹ค. ์šฐ๋ฆฌ๋‚˜๋ผ ๊ฐœ๋ฐœ์ž์˜ ๋Œ€๋ถ€๋ถ„์€ <head> ํƒœ๊ทธ ์˜์—ญ์— ์„ ์–ธํ•˜๊ณ  ์žˆ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>์„ ์–ธ๋ฌธ</title>
	<script>
		document.write("ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค");
	</script>
</head>
<body>
</body>
</html>

document๋Š” ๋ฌธ์„œ ๊ฐ์ฒด์ด๋ฉฐ ๋ฌธ์„œ ์ถœ๋ ฅ ๋ฉ”์„œ๋“œ์ธ write( )๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์„œ์— ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

โ€‹

 

 

โ–ถ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฃผ์„ ์ฒ˜๋ฆฌ

ํ•œ ์ค„ ์ฃผ์„์ผ ๊ฒฝ์šฐ์—๋Š” // ๋กœ, ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„์ผ ๊ฒฝ์šฐ์—๋Š” /* */ ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

 

โ€‹

โ–ถ ๋‚ด๋ถ€ ์Šคํฌ๋ฆฝํŠธ ๋ถ„๋ฆฌํ•˜๊ธฐ

HTML ๋‚ด๋ถ€์— ์ž‘์„ฑ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋งˆ์ง€๋ง‰์—๋Š” ์™ธ๋ถ€๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค ์ฐพ๊ธฐ๋„ ์‰ฌ์šธ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์†Œ์Šค๋ฅผ ์†์ƒ์‹œํ‚ฌ ์—ผ๋ ค๋„ ์—†๋‹ค. ์ฆ‰, ์™ธ๋ถ€๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ์ฃผ๋œ ๋ชฉ์ ์€ ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ๋ฅผ ์›ํ™œํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

 

 

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์™ธ๋ถ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—ฐ๋™ </title>
	<script src="js/example.js"></script>
</head>
<body>
</body>
</html>

 

 

 

 

๋ณ€์ˆ˜

๋ณ€์ˆ˜๋Š” ๋ณ€ํ•˜๋Š” ๋ฐ์ดํ„ฐ(๊ฐ’)๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด๋‹ค. ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ๋Š” ๋‹ค์Œ ๊ธฐ๋ณธํ˜•๊ณผ ๊ฐ™์ด var ํ‚ค์›Œ๋“œ๋ฅผ ๋ณ€์ˆ˜๋ช… ์•ž์— ๋ถ™์ธ๋‹ค. ๋ณ€์ˆ˜์—๋Š” ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๊ธฐ์กด์— ์žˆ๋˜ ๋ฐ์ดํ„ฐ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์—์„œ ์ง€์›Œ์ง€๊ฒŒ ๋œ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์™ธ๋ถ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—ฐ๋™ </title>
	<script>
		var box;
		box = 100;
		box = 30;
		document.write(box);
	</script>
</head>
<body>
</body>
</html>

 

 

โ–ถ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•

๋ฌธ์žํ˜•(String), ์ˆซ์žํ˜•(Number), ๋…ผ๋ฆฌํ˜•(Boolean), ๋นˆ ๋ฐ์ดํ„ฐ(Undefined)๊ฐ€ ์žˆ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>๋…ผ๋ฆฌํ˜• ๋ฐ์ดํ„ฐ</title>
<script>
	var a = true;
	var b = false;
	var c = 10 > 5; //true
	var d = Boolean(null); //false 

	document.write(a, "<br>");
	document.write(b, "<br>");
	document.write(c, "<br>");
	document.write(d, "<br>");
</script>
</head>
<body>
</body>
</html>

 

 

โ–ถ typeof

์ง€์ •๋œ ๋ฐ์ดํ„ฐ ๋˜๋Š” ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ์ž๋ฃŒํ˜•์„ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>๋ฐ์ดํ„ฐ ํƒ€์ž…</title>
<script>
	var num = 100;
	var str = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ"; 

	document.write(typeof num, "<br>");
	document.write(typeof str);
</script>
</head>
<body>
</body>
</html>

 

 

 

 

์—ฐ์‚ฐ์ž

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž์—๋Š” ์‚ฐ์ˆ , ๋ฌธ์ž ๊ฒฐํ•ฉ, ๋Œ€์ž…(๋ณตํ•ฉ ๋Œ€์ž…), ์ฆ๊ฐ, ๋น„๊ต, ๋…ผ๋ฆฌ, ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค.

 

โ–ถ ๋ฌธ์ž ๊ฒฐํ•ฉ ์—ฐ์‚ฐ์ž

ํ”ผ์—ฐ์‚ฐ์ž(์—ฐ์‚ฐ ๋Œ€์ƒ ๋ฐ์ดํ„ฐ)๊ฐ€ ๋ฌธ์žํ˜• ๋ฐ์ดํ„ฐ์ด๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์žํ˜• ๋ฐ์ดํ„ฐ๋กœ ๊ฒฐํ•ฉํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ๋ฌธ์ž๊ฒฐํ•ฉ ์—ฐ์‚ฐ์ž </title>
<script>
	var t1 = "ํ•™๊ต์ข…์ด";
	var t2 = " ๋•ก๋•ก๋•ก ";
	var t3 = 8282;
	var t4 = " ์–ด์„œ ๋ชจ์ด์ž";
	var result;

	result = t1 + t2 + t3 + t4; 
	document.write(result);
</script>
</head>
<body>
</body>
</html>

 

 

โ–ถ ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž

์กฐ๊ฑด์‹(true ๋˜๋Š” false์˜ ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ˜ํ™˜)์˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋กœ, ์—ฐ์‚ฐ์„ ์œ„ํ•ด ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ 3๊ฐœ ํ•„์š”ํ•˜๋‹ค. ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋Š” ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ ์กฐ๊ฑด์‹์˜ ๋งŒ์กฑ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž </title>
<script>
	var a = 10;
	var b = 3;

	var result = a > b ? "javascript" : "hello";
	document.write(result); //jvaascript
</script>
</head>
<body>
</body>
</html>

 

 

์‹ค์Šต) ์ ์ • ์ฒด์ค‘ ๊ตฌํ•˜๊ธฐ

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ๊ณผ์ฒด์ค‘ ํ…Œ์ŠคํŠธ </title>
<script>
	var userHeight = 180;  // ์‹ ์žฅ
	var userWeight = 74;  // ๋ชธ๋ฌด๊ฒŒ
	
	// ํ‰๊ท  ์ฒด์ค‘ = (ํ‚ค - 100) * 0.9
	var normal_w = (userHeight - 100) * 0.9;
	document.write(normal_w);
</script>
</head>
<body>
</body>
</html>

 

 

์‹ค์Šต) ์ ์ • ์ฒด์ค‘ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> ์ ์ • ๋ชธ๋ฌด๊ฒŒ ๊ตฌํ•˜๊ธฐ </title>
<script>
	var name = prompt("๋‹น์‹ ์˜ ์ด๋ฆ„์€?", "");
	var height = prompt("๋‹น์‹ ์˜ ์‹ ์žฅ์€?", "0");
	var weight = prompt("๋‹น์‹ ์˜ ๋ชธ๋ฌด๊ฒŒ๋Š”?", "0");
	
	var normal_w = (height - 100) * 0.9;
	var result = weight >= normal_w - 5 && weight <= normal_w + 5;
	result = result ? "์ ์ • ์ฒด์ค‘์ž…๋‹ˆ๋‹ค." : "์ ์ • ์ฒด์ค‘์ด ์•„๋‹™๋‹ˆ๋‹ค.";
	document.write(name +"๋‹˜์€ "+ result);
</script>
</head>
<body>
</body>
</html>

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€