๐Ÿ„

005 JS(JavaScript)

ย 
HTML์€ ์‚ฌ๋žŒ์˜ ๋ชธ ์ค‘์—์„œ ์‚ฌ๋žŒ์˜ ๋ผˆ๋Œ€์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์˜ ๋ผˆ๋Œ€๋ฅผ ์ž˜ ๋งŒ๋“ค์–ด์•ผ ์‚ฌ๋žŒ์˜ ๊ตฌ์‹ค์„ ์ž˜ ํ•˜๊ฒ ์ฃ ? ์‚ฌ๋žŒ์˜ ํŒ” ํ•˜๋‚˜๊ฐ€ ์—†๊ฑฐ๋‚˜ ๋‹ค๋ฆฌ๊ฐ€ ์—†์œผ๋ฉด ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ HTML์„ ๊ฐ€์žฅ ๋จผ์ € ์ž˜ ๊ตฌ์„ฑ์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
notion imagenotion image
ย 
CSS๋Š” ์‚ฌ๋žŒ์—๊ฒŒ ์˜ท์„ ์ž…ํ˜€์ฃผ๋Š” ๊ฒƒ๊ณผ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ๋ผˆ๋Œ€์— ์–ด๋–ค ์˜ท์„ ์ž…ํžˆ๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ์›น ํŽ˜์ด์ง€๊ฐ€ ์ž˜ ๋งŒ๋“  ์›น ํŽ˜์ด์ง€๊ฐ€ ๋˜๊ฑฐ๋‚˜ ๋ชป ๋งŒ๋“  ์›น ํŽ˜์ด์ง€๊ฐ€ ๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. HTML๊ณผ CSS๋งŒ์„ ํ™œ์šฉํ•˜์—ฌ ์›น์„ ๋งŒ๋“ค๋ฉด ์ •์  ์›น ์‚ฌ์ดํŠธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ผˆ๋Œ€์— ์˜ท์„ ์ž…ํžŒ๋‹ค๊ณ , ์‚ฌ๋žŒ์ด ์›€์ง์ผ ์ˆ˜ ์žˆ์„๊นŒ์š”?
๊ทธ๋ž˜์„œ ์›€์ง์ž„์„ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ JavaScript๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. JavaScript๋ฅผ ํ™œ์šฉํ•ด์„œ ์›น ํŽ˜์ด์ง€์— ์›€์ง์ž„์„ ์ฃผ๋ฉด ์ •์ ์ธ ์‚ฌ์ดํŠธ๊ฐ€ ์•„๋‹Œ ๋™์ ์ธ ์‚ฌ์ดํŠธ๊ฐ€ ๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JavaScript๋ฅผ ํ™œ์šฉํ•ด์„œ ์›น ์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
JAVA์™€ JavaScript๋ฅผ ํ—ท๊ฐˆ๋ คํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด ์žˆ๋Š”๋ฐ JAVA๋Š” ๋ฐฑ์—”๋“œ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด์ด๊ณ , JavaScript๋Š” ํ”„๋ก ํŠธ์—”ํŠธ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด๋กœ ๋‹ค๋ฅธ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, JavaScript ์–ธ์–ด๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด๋กœ ํ•œ ์ค„์”ฉ, ํ•œ ๋ธ”๋ก์”ฉ ์ผ์ฒ˜๋ฆฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ JAVA๋Š” ์ปดํŒŒ์ผ๋Ÿฌ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ ์–ธ์–ด๋Š” ๋ฌธ์„œ๋ฅผ ํ•œ๊บผ๋ฒˆ์— ๋‹ค ์ฝ๊ณ  ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
JavaScript์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์•Œ๊ธฐ ์ „์— ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋กœ์จ ๋ณดํ†ต ๊ทธ๋ฆ‡์— ๋งŽ์ด ๋น„์œ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆ‡์˜ ์ด๋ฆ„์„ ์ •ํ•˜๊ณ , ๊ทธ๋ฆ‡์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 

1. ๋ณ€์ˆ˜ ์„ค์ •

ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> </head> <body> <script> var userName = "์ฐฉํ•œํ˜ธ๋ž‘์ด"; </script> </body> </html>
ย 
์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด "์ฐฉํ•œํ˜ธ๋ž‘์ด" ๋ผ๋Š” ๋ฌธ์ž์—ด์ด userName์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ '='์€ ๋Œ€์ž…์—ฐ์‚ฐ์ž๋กœ ์šฐ๋ณ€์— ์žˆ๋Š” ๊ฐ’์„ ์ขŒ๋ณ€์— ๋„ฃ๊ฒ ๋‹ค๋ผ๋Š” ๋œป์ด ๋ฉ๋‹ˆ๋‹ค.
ย 
์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์ด ์ž˜ ๋‹ด๊ฒผ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ™•์ธ์„ ํ•  ๋•Œ์—๋Š” [๊ฐœ๋ฐœ์ž ๋„๊ตฌ]-[์ฝ˜์†”] ์ฐฝ์— ๊ธฐ๋ก์„ ๋‚จ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 
console.log() ๋ช…๋ น์–ด๋Š” ์ž…๋ ฅ์œผ๋กœ ์ค€ ๊ฐ’์„ ์ฝ˜์†”์ฐฝ์— ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> </head> <body> <script> var userName = "์ฐฉํ•œํ˜ธ๋ž‘์ด"; console.log(userName); </script> </body> </html>
ย 
notion imagenotion image
ย 
var ๋ง๊ณ ๋„ let ๋˜๋Š” const๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • let : ๋ณ€์ˆ˜์•ˆ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ๋„ฃ๊ณ  ๋นผ๊ณ ๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ ๋ณ€ํ˜•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • const : ๋ณ€์ˆ˜์•ˆ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ๋‹ด์•„๋‘๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฅธ๊ฒƒ์ด ์ ˆ๋Œ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
ย 
๋˜ํ•œ typeof ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ ๊ฐ ๋ณ€์ˆ˜๋“ค์˜ ํƒ€์ž…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> </head> <body> <script> // var userName = "์ฐฉํ•œํ˜ธ๋ž‘์ด"; const userName= "์ฐฉํ•œํ˜ธ๋ž‘์ด"; // string ๋ฌธ์ž์—ด("",'') const age = 43; // number ์ˆซ์ž const married = false; // boolean ์ฐธ, ๊ฑฐ์ง“ let girlfriend; // undefined ์ •์˜๋˜์ง€ ์•Š์Œ const money = null; // null ๋น„์–ด์žˆ์Œ // console.log(userName); console.log(typeof userName); console.log(typeof age); console.log(typeof married); console.log(typeof girfrenid); console.log(typeof money); </script> </body> </html>
ย 
notion imagenotion image
ย 
์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด, ์ˆซ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ์ž๋ฃŒํ˜•์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 

2. ์—ฐ์‚ฐ์ž

๋‹ค์Œ์œผ๋กœ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. JavaScript์—์„œ๋Š” ์‚ฐ์ˆ , ์ฆ๊ฐ€, ๊ฐ์†Œ, ๋Œ€์ž…, ๋น„๊ต, ์กฐ๊ฑด, ๋…ผ๋ฆฌ ๋“ฑ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ž๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
ย 
๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž์™€, ๋น„๊ต์—ฐ์‚ฐ์ž๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋ง์…ˆ, ๋บ„์…ˆ, ๊ณฑ์…ˆ, ๋‚˜๋ˆ—์…ˆ ๊ณ„์‚ฐ์„ ํ•˜๋Š” ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค.
  • ๋น„๊ต์—ฐ์‚ฐ์ž๋Š” ์ขŒ๋ณ€๊ณผ ์šฐ๋ณ€์„ ๋น„๊ตํ•˜๋Š” ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. JavaScript์—์„œ๋Š” '='์€ ๋Œ€์ž…์—ฐ์‚ฐ์ž์ด๊ณ  '===' ์€ ๋น„๊ต์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> </head> <body> <script> // var userName = "์ฐฉํ•œํ˜ธ๋ž‘์ด"; const userName= "์ฐฉํ•œํ˜ธ๋ž‘์ด"; // string ๋ฌธ์ž์—ด("",'') const age = 43; // number ์ˆซ์ž const married = false; // boolean ์ฐธ, ๊ฑฐ์ง“ let girlfriend; // undefined ์ •์˜๋˜์ง€ ์•Š์Œ const money = null; // null ๋น„์–ด์žˆ์Œ // console.log(userName); console.log(typeof userName); console.log(typeof age); console.log(typeof married); console.log(typeof girfrenid); console.log(typeof money); // ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž console.log(2-3); console.log(2*3); // ๋น„๊ต ์—ฐ์‚ฐ์ž console.log(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'); console.log(userName === '์ฐฉํ•œํ˜ธ๋ž‘์ด'); console.log(age > 30); console.log(age < 30); </script> </body> </html>
ย 
notion imagenotion image
notion imagenotion image
notion imagenotion image
notion imagenotion image
notion imagenotion image
notion imagenotion image
ย 

3. ์กฐ๊ฑด๋ฌธ

if๋ฌธ

if( 1๋ฒˆ ์กฐ๊ฑด ){ 1๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }
ย 
ํŒŒ์ผ๋ช… : test.html
... // ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž console.log(2-3); console.log(2*3); // ๋น„๊ต ์—ฐ์‚ฐ์ž console.log(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'); console.log(userName === '์ฐฉํ•œํ˜ธ๋ž‘์ด'); console.log(age > 30); console.log(age < 30); if(userName === '์ฐฉํ•œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); } ...
ย 
ํŒŒ์ผ๋ช… : test.html
... /* if(userName === '์ฐฉํ•œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); } */ if(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); } ...
ย 
notion imagenotion image
ย 
์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
ย 

if~else๋ฌธ

if( 1๋ฒˆ ์กฐ๊ฑด ){ 1๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }else{ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }
ย 
ํŒŒ์ผ๋ช… : test.html
... /* if(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); } */ if(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); }else{ console.log('์˜ค๋‹ต!'); } ...
ย 
notion imagenotion image
ย 

if~else if๋ฌธ

if( 1๋ฒˆ ์กฐ๊ฑด ){ 1๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }else if( 2๋ฒˆ ์กฐ๊ฑด ){ 2๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }else if{ 3๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }
ย 
ํŒŒ์ผ๋ช… : test.html
... /* if(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); }else{ console.log('์˜ค๋‹ต!'); } */ if(userName === '์–ด๋ฒ™ํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์–ด๋ฒ™ํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋ฉ์ฒญํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋ฉ์ฒญํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='์ฐฉํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์ฐฉํ•œ ํ˜ธ๋ž‘์ด์•ผ'); } ...
ย 
notion imagenotion image
ย 

if~else if~else๋ฌธ

if( 1๋ฒˆ ์กฐ๊ฑด ){ 1๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }else if( 2๋ฒˆ ์กฐ๊ฑด ){ 2๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }else if( 3๋ฒˆ ์กฐ๊ฑด ){ 3๋ฒˆ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }else{ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ช…๋ น์–ด }
ย 
ํŒŒ์ผ๋ช… : test.html
... /* if(userName === '์–ด๋ฒ™ํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์–ด๋ฒ™ํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋ฉ์ฒญํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋ฉ์ฒญํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='์ฐฉํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์ฐฉํ•œ ํ˜ธ๋ž‘์ด์•ผ'); } */ if(userName === '์–ด๋ฒ™ํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์–ด๋ฒ™ํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋ฉ์ฒญํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋ฉ์ฒญํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋‚˜์œ ํ˜ธ๋ž‘์ด์•ผ'); }else{ console.log('์•ˆ๋…• ๋‚œ ์ฐฉํ•œ ํ˜ธ๋ž‘์ด์•ผ'); } ...
ย 
notion imagenotion image
ย 

4. ๋ฐ˜๋ณต๋ฌธ

for( ์ •์˜ ; ์กฐ๊ฑด ; ๊ฐฑ์‹  ){ ์ˆ˜ํ–‰ ์ฝ”๋“œ }
ย 
ํŒŒ์ผ๋ช… : test.html
... if(userName === '์–ด๋ฒ™ํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์–ด๋ฒ™ํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋ฉ์ฒญํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋ฉ์ฒญํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋‚˜์œ ํ˜ธ๋ž‘์ด์•ผ'); }else{ console.log('์•ˆ๋…• ๋‚œ ์ฐฉํ•œ ํ˜ธ๋ž‘์ด์•ผ'); } for(let i = 0; i< 100; i++){ console.log(i + '๋ฒˆ์ด์•ผ'); } ...
ย 
notion imagenotion image
ย 

5. ์„ ํƒ์ž

์šฐ๋ฆฌ๊ฐ€ HTML ๋งŒ๋“ค๊ณ  ์Šคํƒ€์ผ๋ง์„ ์ž…ํžˆ๊ธฐ ์œ„ํ•ด์„œ id, class์™€ ๊ฐ™์€ ์ด๋ฆ„์ด๋‚˜ ๋ณ„๋ช…์„ ํ†ตํ•ด ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์„ ํƒ์ž๋ฅผ CSS๋กœ ๊ฐ€์ง€๊ณ  ์™”๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ๋“ค ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
JS๋„ ํด๋ฆญํ•ด์„œ ๋™์ ์ธ ์ œ์–ด๋ฅผ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ์„ ํƒ์ž๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. JS๋กœ ์„ ํƒ์ž๋ฅผ ์žก๋Š” ๊ฒฝ์šฐ๋ฅผ '๋” ์˜ค๋ธŒ์ ํŠธ ๊ฐ€์ ธ์˜จ๋‹ค' ๋˜๋Š” '๋” ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ปจํŠธ๋กค ํ•œ๋‹ค' ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋”์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์€ 'document html'๋ฅผ ๋œปํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. <script> ํƒœ๊ทธ ์•ˆ์— JS ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. Selector๋ฅผ ํ†ตํ•ด์„œ HTML์˜ ํƒœ๊ทธ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } </style> </head> <body> <div class="box"></div> <script> ... // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') </script> </body> </html>
ย 
  • getElementsByClassName() ์•ˆ์—๋Š” ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • querySelector() ์•ˆ์—๋Š” CSS์„ ํƒ์ž๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. querySelector์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ํ•˜๋‚˜์˜ ์š”์†Œ๋งŒ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • querySelectorAll()์˜ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
ย 
์ด์–ด์„œ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ•์Šค๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ ๋ฐ•์Šค๊ฐ€ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ์ƒ‰์ด ๋ณ€ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } </style> </head> <body> <div class="box"></div> <script> ... // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') box.addEventListner('click',function(){ console.log('๋ˆŒ๋ €์–ด'); )} // ํด๋ฆญ ์ด๋ฒคํŠธ ์ถ”๊ฐ€ </script> </body> </html>
ย 
notion imagenotion image
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } </style> </head> <body> <div class="box"></div> <script> ... // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') box.addEventListner('click',function(){ // console.log('๋ˆŒ๋ €์–ด'); box.style.background = "blue"; // ํŒŒ๋ž‘์ƒ‰์œผ๋กœ ์ƒ‰์ƒ ๋ณ€๊ฒฝ )} // ํด๋ฆญ ์ด๋ฒคํŠธ ์ถ”๊ฐ€ </script> </body> </html>
ย 
notion imagenotion image
notion imagenotion image
ย 
ํด๋ฆญํ–ˆ์„ ๊ฒฝ์šฐ ๊ฐ‘์ž๊ธฐ CSS์˜ Background๊ฐ€ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ JS์œผ๋กœ CSS๋ฅผ ์ปจํŠธ๋กค ํ•˜๊ฒŒ ๋˜๋ฉด CSS๋Š” ๋ฐ”๋€Œ์ง€ ์•Š๊ณ  ํƒœ๊ทธ์˜ ์ธ๋ผ์ธ ๋ฐฉ์‹์œผ๋กœ style์ด ๋“ค์–ด์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ”๊พธ์ง€ ์•Š๊ณ  ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด ์Šคํƒ€์ผ ์‹œํŠธ์˜ ํŠน์ง•์ธ ์ผ€์Šค์ผ€์ด๋”ฉ, ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ง€์ •ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์šฐ์„ ์ˆœ์œ„ ์ค‘ ์ธ๋ผ์ธ ๋ฐฉ์‹์ด ์™ธ๋ถ€๋กœ ์ž‘์„ฑํ•œ ๊ฒƒ๋ณด๋‹ค ๋†’์Šต๋‹ˆ๋‹ค.
ย 
ํด๋ž˜์Šค ๋ฐ•์Šค์— on์ด๋ผ๊ณ  ์น  ๊ฒฝ์šฐ ์ƒ‰๊น”์ด ๋ฐ”๋€Œ๋„๋ก ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } .box.on{ /* ํด๋ž˜์Šค ๋ฐ•์Šค์™€ on์ด๋ผ๋Š” ์†์„ฑ์„ ํ•จ๊ป˜ ๊ฐ€์ง */ background:green; } </style> </head> <body> <div class="box"></div> <script> ... // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') box.addEventListner('click',function(){ // console.log('๋ˆŒ๋ €์–ด'); box.style.background = "blue"; // ํŒŒ๋ž‘์ƒ‰์œผ๋กœ ์ƒ‰์ƒ ๋ณ€๊ฒฝ )} // ํด๋ฆญ ์ด๋ฒคํŠธ ์ถ”๊ฐ€ </script> </body> </html>
ย 
notion imagenotion image
ย 
notion imagenotion image
ย 
์ด๋ ‡๊ฒŒ box on์ด๋ผ๊ณ  ์น˜๋Š” ์ˆœ๊ฐ„ ์ƒ‰์ƒ์ด ๋ฐ”๋€Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ์Šคํƒ€์ผ ์‹œํŠธ ๋‚ด์—์„œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋”ฐ์งˆ ๊ฒฝ์šฐ์—๋Š” ๋ฐ‘์—์„œ ๋ถ€ํ„ฐ ์œ„๋กœ, ํด๋ž˜์Šค๋‚˜ ์„ ํƒ์ž๊ฐ€ ๋” ๋งŽ์€ ํด๋ž˜์Šค์—๊ฒŒ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค.
ย 
ํด๋ž˜์Šค๋ฅผ ๋„ฃ๋‹ค ๋บ๋‹ค ํ•  ์ˆ˜ ์žˆ๋Š” addํด๋ž˜์Šค์™€ removeํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } .box.on{ /* ํด๋ž˜์Šค ๋ฐ•์Šค์™€ on์ด๋ผ๋Š” ์†์„ฑ์„ ํ•จ๊ป˜ ๊ฐ€์ง */ background:green; } </style> </head> <body> <div class="box"></div> <script> ... // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') box.addEventListner('click',function(){ // console.log('๋ˆŒ๋ €์–ด'); // box.style.background = "blue"; // ํŒŒ๋ž‘์ƒ‰์œผ๋กœ ์ƒ‰์ƒ ๋ณ€๊ฒฝ box.classList.add('on'); )} // ํด๋ฆญ ์ด๋ฒคํŠธ ์ถ”๊ฐ€ </script> </body> </html>
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } .box.on{ /* ํด๋ž˜์Šค ๋ฐ•์Šค์™€ on์ด๋ผ๋Š” ์†์„ฑ์„ ํ•จ๊ป˜ ๊ฐ€์ง */ background:green; } </style> </head> <body> <div class="box"></div> <script> ... // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') box.addEventListner('click',function(){ // console.log('๋ˆŒ๋ €์–ด'); // box.style.background = "blue"; // ํŒŒ๋ž‘์ƒ‰์œผ๋กœ ์ƒ‰์ƒ ๋ณ€๊ฒฝ // box.classList.add('on'); box.classList.remove('on'); )} // ํด๋ฆญ ์ด๋ฒคํŠธ ์ถ”๊ฐ€ </script> </body> </html>
ย 
๋˜ํ•œ ํด๋ž˜์Šค ๋ฆฌ์ŠคํŠธ์™€ ํ† ๊ธ€์„ ํ†ตํ•ด์„œ ์Šค์œ„์น˜์ฒ˜๋Ÿผ ์ƒ‰์„ ์ „ํ™˜ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } .box.on{ /* ํด๋ž˜์Šค ๋ฐ•์Šค์™€ on์ด๋ผ๋Š” ์†์„ฑ์„ ํ•จ๊ป˜ ๊ฐ€์ง */ background:green; } </style> </head> <body> <div class="box"></div> <script> ... // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') box.addEventListner('click',function(){ // console.log('๋ˆŒ๋ €์–ด'); // box.style.background = "blue"; // ํŒŒ๋ž‘์ƒ‰์œผ๋กœ ์ƒ‰์ƒ ๋ณ€๊ฒฝ // box.classList.add('on'); // box.classList.remove('on'); box.classList.toggle('on'); // ์žˆ์œผ๋ฉด .on ํด๋ž˜์Šค ์ œ๊ฑฐ, ์—†์œผ๋ฉด .on ํด๋ž˜์Šค ์ถ”๊ฐ€ )} // ํด๋ฆญ ์ด๋ฒคํŠธ ์ถ”๊ฐ€ </script> </body> </html>
ย 
์™„์„ฑ๋œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
ย 
ํŒŒ์ผ๋ช… : test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript_test</title> <style> .box{ width:200px; height:200px; background: red; } .box.on{ /* ํด๋ž˜์Šค ๋ฐ•์Šค์™€ on์ด๋ผ๋Š” ์†์„ฑ์„ ํ•จ๊ป˜ ๊ฐ€์ง */ background:green; } </style> </head> <body> <div class="box"></div> <script> // var userName = "์ฐฉํ•œํ˜ธ๋ž‘์ด"; const userName= "์ฐฉํ•œํ˜ธ๋ž‘์ด"; // string ๋ฌธ์ž์—ด("",'') const age = 43; // number ์ˆซ์ž const married = false; // boolean ์ฐธ, ๊ฑฐ์ง“ let girlfriend; // undefined ์ •์˜๋˜์ง€ ์•Š์Œ const money = null; // null ๋น„์–ด์žˆ์Œ // console.log(userName); console.log(typeof userName); console.log(typeof age); console.log(typeof married); console.log(typeof girfrenid); console.log(typeof money); // ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž console.log(2-3); console.log(2*3); // ๋น„๊ต ์—ฐ์‚ฐ์ž console.log(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'); console.log(userName === '์ฐฉํ•œํ˜ธ๋ž‘์ด'); console.log(age > 30); console.log(age < 30); /* if(userName === '์ฐฉํ•œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); } */ /* if(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); } */ /* if(userName === '๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์ •๋‹ต!'); }else{ console.log('์˜ค๋‹ต!'); } */ /* if(userName === '์–ด๋ฒ™ํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์–ด๋ฒ™ํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋ฉ์ฒญํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋ฉ์ฒญํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='์ฐฉํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์ฐฉํ•œ ํ˜ธ๋ž‘์ด์•ผ'); } */ if(userName === '์–ด๋ฒ™ํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ์–ด๋ฒ™ํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋ฉ์ฒญํ•œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋ฉ์ฒญํ•œ ํ˜ธ๋ž‘์ด์•ผ'); }else if(userName ==='๋‚˜์œํ˜ธ๋ž‘์ด'){ console.log('์•ˆ๋…• ๋‚œ ๋‚˜์œ ํ˜ธ๋ž‘์ด์•ผ'); }else{ console.log('์•ˆ๋…• ๋‚œ ์ฐฉํ•œ ํ˜ธ๋ž‘์ด์•ผ'); } for(let i = 0; i< 100; i++){ console.log(i + '๋ฒˆ์ด์•ผ'); } // document.getElementsByClassName('box'); const box = documment.querySelector('.box') // const box = documment.querySelectorAll('.box') box.addEventListner('click',function(){ // console.log('๋ˆŒ๋ €์–ด'); box.style.background = "blue"; // ํŒŒ๋ž‘์ƒ‰์œผ๋กœ ์ƒ‰์ƒ ๋ณ€๊ฒฝ // box.classList.add('on'); // box.classList.remove('on'); box.classList.toggle('on'); // ์žˆ์œผ๋ฉด .on ํด๋ž˜์Šค ์ œ๊ฑฐ, ์—†์œผ๋ฉด .on ํด๋ž˜์Šค ์ถ”๊ฐ€ )} // ํด๋ฆญ ์ด๋ฒคํŠธ ์ถ”๊ฐ€ </script> </body> </html>