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

[TIL] ๐Ÿ’กDay 2 - JS์˜ ๋ณ€์ˆ˜, ํƒ€์ž…, Array, Object

by vividmin 2022. 3. 11.
320x100

โœ” ์ด ํฌ์ŠคํŒ…์€ ๋…ธ๋งˆ๋“œ์ฝ”๋” - "๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ๋ฅผ ๋“ค์œผ๋ฉด์„œ ๊นƒํ—ˆ๋ธŒ์— ๊ธฐ๋กํ•œ ํ•„๊ธฐ๋ฅผ ์˜ฎ๊ธด ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

๐Ÿ“‘ JS์˜ ๋ณ€์ˆ˜, ํƒ€์ž…, Array, Object์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋‹ค.


๊ฒŒ์œผ๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ ๋…ธ๋ ฅํ•˜์ž → ๋ฐ˜๋ณต ์ค„์ด๊ธฐ!๐Ÿ‘ฉ‍๐Ÿ’ป  

 

๐ŸŒฑ ๋ณ€์ˆ˜

๐Ÿ‘‰ variable, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์ˆ˜

  • ex. 2 + 3 or 2 * 3 ๊ฐ™์€ ์—ฐ์‚ฐ ์‹œ ์ˆซ์ž๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•  ์ผ์ด ์ƒ๊ธฐ๋ฉด ๋งค๋ฒˆ ์ˆ˜์ •ํ•ด์•ผ ๋˜๋Š”๋ฐ ์ด ๊ฒƒ์€ ๋น„ ํšจ์œจ์ ์ด๋‹ค!๐Ÿค”
  • ์ด๋•Œ ๋ณ€์ˆ˜์— ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜๊ณ  a + b ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ๊ณ„์‚ฐ์„ ํ•˜๋ฉด ๋ณ€์ˆ˜๋งŒ ๋ฐ”๊พธ๋ฉด ๋˜๋‹ˆ๊นŒ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค!๐Ÿค—
  • const, let์„ ํ†ตํ•ด์„œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  • ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ X → const / ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๊ฒƒ → let => ์ด๊ฒƒ๋งŒ ๋ด๋„ ์ž‘์„ฑ์ž์˜ ์˜๋„๊ฐ€ ํŒŒ์•… ๊ฐ€๋Šฅํ•˜๋‹ค.
  • JS๋Š” 10์ผ ๋งŒ์— ๋งŒ๋“ค์–ด์„œ ํŒจ์น˜๊ฐ€ ๋งŽ์ด ๋˜์—ˆ๋‹ค. ํŒจ์น˜ ์ „์—๋Š” var๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ–ˆ๋‹ค.
  • var๋Š” let๊ณผ const ๊ฐ™์€ ๊ทœ์น™์ด ์—†์–ด์„œ ์ž‘์„ฑ์ž์˜ ์˜๋„๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ต๊ณ , ๋ณ€ํ•˜์ง€ ๋ง์•„์•ผ ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์œ„ํ—˜ํ•˜๋‹ค.๐Ÿ˜ฑ
  • ๐Ÿ“Œ always const, sometimes let, never var!!! ๐Ÿ“Œ

 

 

๐ŸŒฑ ๋ณ€์ˆ˜์˜ ํƒ€์ž…

๐Ÿ‘‰ number, string, boolean, null, undefined๊ฐ€ ์กด์žฌํ•œ๋‹ค.

  1. number integer → 2 / float → 2.0 ๊ฐ€ ์กด์žฌํ•˜๋ฉฐ int + float = float์ด๋‹ค.
  2. string "" , ''์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์•ž ๋’ค๋ฅผ ๋™์ผํ•œ ๊ธฐํ˜ธ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  3. boolean true | false ("true" ์ด๋ ‡๊ฒŒ ์“ฐ๋ฉด ์•ˆ ๋œ๋‹ค, ์ปดํ“จํ„ฐ์˜ 0 = false, 1 = true)
  4. null '์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค'๋ผ๋Š” ์˜๋ฏธ, '์ ˆ๋Œ€ ์ž์—ฐ์ ์œผ๋กœ ์ƒ๊ธฐ์ง€ ์•Š์œผ๋ฉฐ ์šฐ๋ฆฌ๊ฐ€ variable์•ˆ์— ์–ด๋–ค ๊ฒƒ์ด ์—†๋‹ค๋Š” ๊ฒƒ์„ ํ™•์‹คํžˆ ํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ๋Š” ๊ฒƒ'
  5. undefined ์กด์žฌํ•˜์ง€๋งŒ ์ •์˜๋˜์ง€ ์•Š์•˜๋‹ค ๋ผ๋Š” ์˜๋ฏธ

 

 

๐ŸŒฑ Array [ ]

๐Ÿ‘‰ ํ•˜๋‚˜์˜ variable์•ˆ์— ๋ฐ์ดํ„ฐ์˜ list๋ฅผ ๊ฐ€์ง€๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ๋‹ค.

  • ๋น„ ํšจ์œจ์  ์˜ˆ์‹œ๐Ÿ™…‍โ™€๏ธ
    • ๋ฐ˜๋ณต ๋„ˆ๋ฌด ๋งŽ๊ณ , ์›ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฐพ๊ธฐ๋„ ์–ด๋ ค์›€
const tue = "tue";      // not good
const wed = "wed";
const thu = "thu";
const fri = "fri";
const sat = "sat";
const sun = "sun";

// not good
const daysOfWeek = " mon + tue + wed + thu + fri + sat + sun";
  • ํšจ์œจ์  ์˜ˆ์‹œ๐Ÿ™†‍โ™€๏ธ
    • Array ์ด์šฉํ•˜์—ฌ data ๋‹ด๊ธฐ → ๋ฐ˜๋ณต↓ , ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ ์‰ฌ์›€, ์ถ”๊ฐ€๋„ ๊ฐ€๋Šฅ 
const daysOfWeek = [ "mon" , "tue" , "wed" , "thu" , "fri" , "sat"];     // good

const toBuy = ["potato", "tomato", "apple"];

// Get Item (fri?)
console.log(daysOfWeek[4]);

// Push Item
daysOfWeek.push("sun");
toBuy.push("kimbab");

 

๐ŸŒฑ Object { }

๐Ÿ‘‰ ์„ค๋ช…์ด ํ•„์š”ํ•œ data๋“ค์„ ๋‚˜์—ดํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ

  • Object๊ฐ€ ํ•„์š”ํ•œ ์˜ˆ์‹œ ๐Ÿ‘€
// player๋ผ๋Š” ๊ณตํ†ต์ ์ด ์žˆ์Œ 
const playerName = "jm"; 
const palyerPoints = 121212; 
const playerCool = true; 
const playerRich = "little bit"; 

const player = ["jm", 1212, true, "little bit"]; 
// array ๋กœ๋Š” ๋ช…ํ™•ํ•œ ์˜๋ฏธ๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
  • player๋ผ๋Š” ๊ณตํ†ต์ ๊ณผ ๊ฐ๊ฐ์˜ ์˜๋ฏธ๋ฅผ ๊ฐ™์ด ํ‘œํ˜„ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. โฌ
// Make object? 
    const player = { 
    name: 'jm', 
    points: 121212, 
    cool: true, 
    rich: "little bit", 
}; 
console.log(player); // ์ „์ฒด object ์ถœ๋ ฅ 
console.log(player.name); // object์˜ name property๋ฅผ ์ถœ๋ ฅ 
console.log(player["name"]);
  • player.name์ฒ˜๋Ÿผ console.log๋ฅผ ์“ฐ๊ณ  ์žˆ๋‹ค ๊ทธ ๋ง์€ console๋„ ๊ฐ์ฒด๋ผ๋Š” ์˜๋ฏธ
  • ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ list๊ฐ€ ์•„๋‹ˆ๋‹ค, list๋Š” ๋ชจ๋‘ ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ
ex. ํ•œ ์ฃผ์˜ ์š”์ผ = [์›” ~ ์ผ] or ์žฅ๋ณด๊ธฐ ๋ฆฌ์ŠคํŠธ = [๐ŸŽ, ๐Ÿง…, ๐Ÿฅ•]
-> ์•ˆ์˜ ๋ชจ๋“  ๋‚ด์šฉ์ด ์š”์ผ or ์žฅ๋ณด๊ธฐ ๋ฆฌ์ŠคํŠธ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Œ
ํ•˜์ง€๋งŒ player { }๋งŒ ๋ณด๋ฉด ์•ˆ์˜ ๋‚ด์šฉ์ด ๋‹ค ๋‹ค๋ฆ„, ๊ทธ๋ƒฅ jm, 1212๋งŒ ๋ณด๊ณ ๋Š” ๋ญ”์ง€ ์•Œ ์ˆ˜ ์—†์Œ
๋”ฐ๋ผ์„œ player.name player.points๋ผ๊ณ  ํ•ด์•ผ ์˜๋ฏธ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Œ.
  • const object์ธ๋ฐ ๋‚ด์šฉ ์ˆ˜์ • ๊ฐ€๋Šฅโ“
    • ์œ„ ๋‘˜์˜ ๋‚ด์šฉ์ด ๋‹ค๋ฆ„, const์ธ๋ฐ ์™œ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ๊ฐ€?๐Ÿค”
    • const player = objcet์ด๋‹ค. ๋”ฐ๋ผ์„œ ์•ˆ์˜ ๋‚ด์šฉ์„ ๋ฐ”๊พธ์–ด๋„ ์—ฌ์ „ํžˆ player๋Š” object์ด๋‹ค.
    • ์ฆ‰, object์˜ ์•ˆ์˜ ๋‚ด์šฉ์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ ์ƒ๊ด€์—†์Œ
console.log(player.cool); // true 
player.cool = false; 
console.log(player.cool); // false
  • property๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
player.lastName = "potato"; 
console.log(player); 
/*
player = { 
    name: 'jm', 
    points: 121212, 
    cool: true, 
    rich: "little bit", 
    lastName: "potato" 
}; 
/
  • array vs object
    • ์„ค๋ช…์ด ํ•„์š” ์—†๋Š” ๊ฒƒ ๐Ÿ‘‰ array
    • ์„ค๋ช…์ด ํ•„์š”ํ•œ ๊ฒƒ์€ ๐Ÿ‘‰ object

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€