DEV

JavaScript μ‹œμž‘ν•˜κΈ°

camille: 2024. 10. 20. 17:23
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
λ°˜μ‘ν˜•