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

JavaScript ์‹œ์ž‘ํ•˜๊ธฐ

by camille: 2024. 10. 20.
728x90
๋ฐ˜์‘ํ˜•

1. ๐Ÿ“’ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ž€?

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript)๋Š” ๊ฐ์ฒด(object) ๊ธฐ๋ฐ˜์˜ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์ด๋‹ค.
HTML๋กœ๋Š” ์›น์˜ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•˜๊ณ , CSS๋กœ๋Š” ์›น์„ ๋””์ž์ธํ•˜๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ๋Š” ์›น์˜ ๋™์ž‘์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ฃผ๋กœ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‚ฌ์šฉ๋˜๋‚˜, Node.js์™€ ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋ฒ„ ์ธก ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋˜ํ•œ, ํ˜„์žฌ ์ปดํ“จํ„ฐ๋‚˜ ์Šค๋งˆํŠธํฐ ๋“ฑ์— ํฌํ•จ๋œ ๋Œ€๋ถ€๋ถ„์˜ ์›น ๋ธŒ๋ผ์šฐ์ €์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ๋‚ด์žฅ๋˜์–ด ์žˆ๋‹ค.

2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์ง•

  1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ์ฒด ๊ธฐ๋ฐ˜์˜ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์ด๋‹ค.
  2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋™์ ์ด๋ฉฐ, ํƒ€์ž…์„ ๋ช…์‹œํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด์ด๋‹ค.
  3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋ชจ๋‘ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ” C์–ธ์–ด์™€ ๊ฐ™์€ ์–ธ์–ด๋Š” ์†Œ์Šค ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ ํ›„, ํ•ด๋‹น ํŒŒ์ผ์„ ์ปดํŒŒ์ผ(compile)ํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์‹คํ–‰ ํŒŒ์ผ(.exe)๋กœ ๋งŒ๋“ค์–ด ํ•˜์ง€๋งŒ, ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด๋Š” ์ด๋Ÿฌํ•œ ์ปดํŒŒ์ผ ์ž‘์—…์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ , ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ฐ”๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์— ํฌํ•จ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ํ•ด์„ํ•˜์—ฌ ๋ฐ”๋กœ ์‹คํ–‰ํ•ด ์ค€๋‹ค.

3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

๐Ÿ“Œ 1. Console.log

console.log๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋””๋ฒ„๊น…(์ถœ๋ ฅ)๋ฐฉ๋ฒ•์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ถœ๋ ฅ์ด๋ž€ ๊ฐ’์„ ๋ˆˆ์— ๋ณด์ด๋„๋ก ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด๋‹ค.
โœ

console.log("camille");

๊ฐœ๋ฐœ์ž๋“ค ์‚ฌ์ด์—์„œ๋Š” ํ”ํžˆ "์ฐ์–ด๋ณธ๋‹ค."๋ผ๋Š” ํ‘œํ˜„์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด๋Š” ์ฝ˜์†”๋กœ๊ทธ๋กœ ํŠน์ • ๊ฐ’์„ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚ธ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

"์ฝ˜์†”์— ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธด๋‹ค." ์—ฌ๊ธฐ์„œ ์ฝ˜์†”์€ ์ฝ˜์†” ํƒญ์„ ์˜๋ฏธ ํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์›๋ž˜ ์ฝ˜์†”์˜ ์˜๋ฏธ๋Š” ๋ธŒ๋ผ์šฐ์ € ์•ˆ์— ๋‹ด๊ฒจ์žˆ๋Š”๋ฐ ๋ธŒ๋ผ์šฐ์ €์ƒ์— ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ๋’ค, ์ธ์ŠคํŽ™ํŠธ(๊ฒ€์‚ฌ) ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ์˜ค๋ฅธ์ชฝ ์œ„ ์ฝ˜์†”์ด๋ผ๋Š” ํƒญ์ด ๋‚˜ํƒ€๋‚œ๋‹ค.

๊ธฐ๋ณธํ˜• : console.log(์ถœ๋ ฅ ๋Œ€์ƒ);

๐Ÿ“Œ 2. Variable (๋ณ€์ˆ˜)

๋ณ€์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋Œ€์ƒ์„ ์˜๋ฏธํ•œ๋‹ค.
โœ

let myNumber = 500;

์—ฌ๊ธฐ์„œ myNumber๋ผ๋Š” ๋ณ€์ˆ˜์— ์ˆซ์ž 500์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์—ฌ๊ธฐ์„œ myNumber์€ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๋˜๋ฉฐ, 500์€ ๋ณ€์ˆ˜์— ๋‹ด๊ธด ๊ฐ’์ด ๋œ๋‹ค.

๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์•„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‘๊ฐ€์ง€ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์•ผ ํ•˜๋Š”๋ฐ, ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. let myNumber ์ด๋ฆ„์„ ์„ ์–ธ(Declare)ํ•ด ์ค€๋‹ค.
  2. let myNumber = 500; ๋ฐ์ดํ„ฐ๋ฅผ ํ• ๋‹น(or๋Œ€์ž…) ํ•ด ์ค€๋‹ค.
    = ์ด์ œ๋ถ€ํ„ฐ myName์€ 500๋ผ๋Š” ๊ฐ’์„ ๋Œ€์‹  ์ง€์นญํ•˜๊ฒŒ ๋œ๋‹ค.

let myNumber = 5;
let myNumber = 5;
์œ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋ณ€์ˆ˜(variable)์— ๊ฐ’์„ ํ• ๋‹นํ•  ๋•Œ ๋Œ€์ž…์—ฐ์‚ฐ์ž(=)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๋Œ€์ž…์—ฐ์‚ฐ์ž๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์˜ค๋ฅธ์ชฝ ํ•ญ์— ์žˆ๋Š” ๊ฐ’์„ ์™ผ์ชฝ์— ์žˆ๋Š” ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๋Š” ์—ญํ• ํ•œ๋‹ค.
let myVar = 5;
let myNum = myVar;

๐Ÿ‘ฉโ€๐Ÿซnull & undefined

null๊ณผ undefined๋Š” ๋ชจ๋‘ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.
undefined๋Š” ์„ ์–ธ์€ ๋์ง€๋งŒ ์•„์ง value๊ฐ€ ํ• ๋‹น๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ,
null์€ '๋นˆ ๊ฐ’(blank)'์„ ์˜๋ฏธํ•˜๋Š”๋ฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ค€ value์ด๋‹ค.
๊ทธ๋ž˜์„œ undefined์™€ ๋‹ค๋ฅด๊ฒŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž๋™์ ์œผ๋กœ null ์ด๋ž€ ๊ฐ’์„ ์ค„ ์ˆ˜๋Š” ์—†๋‹ค.
ํฌ๊ด„์ ์ธ ์˜๋ฏธ๋กœ '๊ฐ’์ด ์—†๋‹ค'๋Š” ์ ์—์„œ null๊ณผ undefined๊ฐ€ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™์ง€๋งŒ ๋‘˜์€ ์—„๊ฒฉํ•˜๊ฒŒ๋Š” ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

let name;            // undefined
let name = null;     // null

console.log(null == undefined);   // true
console.log(null === undefined);  // false
let name;            // undefined
let name = null;     // null

console.log(null == undefined);   // true
console.log(null === undefined);  // false

์—„๊ฒฉ์ผ์น˜์—ฐ์‚ฐ(===)๋Š” value๋ฟ๋งŒ ์•„๋‹ˆ๋ผ type๋„ ๊ฐ™์•„์•ผ true๊ฐ€ ๋‚˜์˜จ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด null๊ณผ undefined์˜ type์€ ์–ด๋–ค ํƒ€์ž…์ผ๊นŒ? ํƒ€์ž…์„ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด์„œ
typeof ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž.

console.log(typeof null);       // object
console.log(typeof undefined);  // undefined
console.log(typeof null);       // object
console.log(typeof undefined);  // undefined

null์˜ type์ด object๊ฐ€ ๋‚˜์™”๋Š”๋ฐ, ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ?? null์€ ์œ„์— ์„ค๋ช…ํ•œ๋Œ€๋กœ '๊ฐ’์ด ์—†์Œ(blank)'์„ ์˜๋ฏธํ•˜๋Š” 'ํ• ๋‹น๋œ' value์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๐Ÿ“Œ 3. ๋ฐ์ดํ„ฐ ํƒ€์ž…

๊ธฐ๋ณธํƒ€์ž…(Primitive type) : Number, String, Boolean, Undefinde, Null
์ฐธ์กฐํƒ€์ž…(Reference typr) : Object, Array, Function

โœ 1) Number

: 0, 1, 500, 1000 ยทยทยท ์ˆซ์ž ํƒ€์ž… ๋ชจ๋‘

let myNumber1 = 2;
let myNumber2 = 100;
let myNumber3 = 400;

console.log(myNumber2); 
< 400

console.log(myNumber2 + myNumber3)
< 500
8
console.log(myNumber1 * myNumber3)
< 800

console.log(myNumber1 % muNumber3)
< 2
  • ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ %๋Š” ์•ž์˜ ์ˆซ์ž๋ฅผ ๋’ค์˜ ์ˆซ์ž๋กœ ๋‚˜๋ˆˆ ๋’ค ๋‚˜๋จธ์ง€๋งŒ ๊ตฌํ•œ๋‹ค.

โœ 2) String

: 'camille', 'dog', 'peach' ยทยทยท์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž์—ด ๋ชจ๋‘

console.log("Cat");
< Cat

console.log("Graph");
< Graph

let myName = "Camille";
console.log(myName);
< Camille

let myNumber1 = 2;
let myNumber2 = 100;
console.log('2 plus 100 equal to ' myNumber1 + myNumber2);
< 2 plus 100 equals to 102

๐Ÿ‘‰ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ String์„ ๋”ํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•

  1. +operator

let a ='old';
let b = 'tree';
let c = a + b;
console.log(c);
< old tree
    • operator2
      let msg = 'There are';
      mag += 'three falcons';
      msg += 'in the sky'
      console.log(msg);
      < There are three three falcons in the sky
  1. join method
    let words = ['There', 'are', 'three', 'falcons', 'in', 'the', 'sky'];
    let msg = word.join(' ');
    console.log(msg);
    <There are three three falcons in the sky
  2. concat method
    let a = 'old';
    let c = a.concat(`tree`);
    console.log(c);
  3. fotmatting strings
    let w1 = 'two';
    let w2 ='eagles';
    let msg = 'Threr are ${w1} ${w2} in the sky';
    console.log(msg);
    <There are two eagles in the sky

โœ” concat ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ๋ฒ”์œ„ ๋ฐ ๋ฌธ์ž์—ด์˜ ํ…์ŠคํŠธ๋ฅผ ๊ฒฐํ•ฉํ•˜์ง€๋งŒ, ๋””๋ฆฌ๋ฏธํ„ฐ ๋˜๋Š” ignoreEmpty์ธ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด =CONCAT("The"," ","sun"," ","will"," ","come"," ","up"," ","tomorrow.")๋Š” The sun will come up tomorrow. ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

โœ” ๋ฌธ์ž์—ด์˜ ์ด ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

const strings = 'Hello! world!!'
console.log(strings.length);
< 14
console.log("The length of pepsi is",'pepsi'.length)
<The length of pepsi is 5
5๋Š” ์ˆซ์ž

console.log("The length of pepsi is" + 'pepsi'.length)
<The length of pepsi is 5
( ๋ฌธ์ž์—ด + ์ˆซ์ž = ๋ฌธ์ž์—ด๋กœ ) 5๋Š” ์ŠคํŠธ๋ง

โœ 3) Boolean

: true, false ์ฐธ๊ณผ ๊ฑฐ์ง“ ๋งž๋Š”์ง€ ํ‹€๋ ธ๋Š”์ง€ ํ™•์ธ ํ•  ๋•Œ ์‚ฌ์šฉ

let myAnswer = true;
let yourAnswer = false;
console.log(myAnswer);
< true

โœ 4) Array

: [1, 2, 3, 4, 5, 6, 7] ๊ฐ’์ด ์ผ๋ ฌ๋กœ ๋ชจ์—ฌ์žˆ๋Š”๋ฐ, ๋ฐฐ์—ด๋กœ ์ผ๋ฐ˜์ ์ธ ์ˆซ์ž, ํ…์ŠคํŠธ, ๋ถˆ๋ฆฌ์–ธ ๋ชจ๋‘ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Œ

let fruit =['apple', 'peach', 'banana'];

console.log(fruit);
<['apple', 'peach', 'banana']

โœ 5) Object

:
์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ์—ฌ์žˆ๋Š” ๋ฌถ์Œ ์œผ๋กœ ํ‚ค ์•ž์—์ด๋ฆ„์„ ๊ผญ ๋ถ™์—ฌ์•ผํ•จ ์‹œ์ž‘์„ ์ค‘๊ด„ํ˜ธ๋กœ

let camille = {
  name: camille son,
  live: daejeon
  };

console.log(camille);
< name:camille son, live: daejeon

โœ 6) Function

: ํ•จ์ˆ˜๋ผ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ๋ถ™์ด๊ณ  ์ค‘๊ด„ํ˜ธ ์•ˆ์—๋Š” ํ•จ์ˆ˜๊ฐ€ ๋‹ด๋‹นํ•  ๊ธฐ๋Šฅ์ด ๋“ค์–ด๊ฐ€ ์žˆ์Œ

  function camille() {
    let petName = "bbachon",
    console.log(petName)
    return petName
    };
728x90
๋ฐ˜์‘ํ˜•