Javascript ์ •๋ฆฌ(5)

kirin.logยท2021๋…„ 2์›” 3์ผ
0

โœ… ๋ฐ์ดํ„ฐ ํƒ€์ž…

๐Ÿ‘‰ ES6์—์„œ ์ฑ„ํƒํ•œ 7๊ฐœ์˜ ๋ฐ์ดํ„ฐํƒ€์ž…

๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(Primitive)

  • String(๋ฌธ์ž์—ด)
  • Number(์ˆซ์ž)
  • Boolean (true/false)
  • Undefined
  • Null
  • Symbol

๊ฐ์ฒด ์ž๋ฃŒํ˜•(Object)

  • Object

๐Ÿ‘‰ typeof ์—ฐ์‚ฐ์ž
๊ฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํ™•์ธํ•˜๋Š” ์—ฐ์‚ฐ์ž
typeof ( );

let msg = "message";
let num = 123;
let confirm = true;

console.log(typeof msg); // "string"
console.log(typeof num); // "number"
console.log(typeof confirm); // "boolean"

console.log(typeof 100); // "number" (์ˆซ์ž(100)๋ฅผ ๋ฐ”๋กœ ๋Œ€์ž…ํ•ด๋„ ํ•ด๋‹น๊ฐ’์˜ ๋ฐ์ดํ„ฐํƒ€์ž…์ด ์ถœ๋ ฅ
console.log(typeof null); // "object"
โ— null ์€ ๋นˆ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—

console.log(typeof []); // "object"
โ— ๋ฐฐ์—ด์€ ํ™•์žฅ๋œ ๊ฐ์ฒด ์ด๊ธฐ ๋•Œ๋ฌธ์—

console.log(typeof (!0)); // "boolean"
console.log(typeof function() {});  // "function"

๐Ÿ‘‰ ๋ฌธ์ž์—ด

// ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€์ˆ˜ ์„ ์–ธ
let name = "leedev";

// .length >> ๋ฌธ์ž์—ด์ด ๋ช‡ ๊ธ€์ž๋กœ ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
name.length // 6

// .toUpperCase >> ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ์ถœ๋ ฅ
name.toUpperCase() // "LEEDEV"

// .indexOf >> ํŠน์ • ํ…์ŠคํŠธ์˜ ํฌํ•จ ์œ ๋ฌด ๋ฐ ์œ„์น˜ ํ™•์ธ
name.indexOf('e') // 1 (1๋ฒˆ์งธ์— e ์œ„์น˜)
name.indexOf('k') // -1 (ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์—†์œผ๋ฉด -1 ๋ฐ˜ํ™˜ํ•œ๋‹ค) โœจ

// String์€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•œ๋‹ค
console.log('js' === 'js'); // true
console.log('Js' === 'js'); // false

โฉ String ๋Œ€์†Œ๋ฌธ์ž ๋ฐ”๊พธ๊ธฐ ํ•จ์ˆ˜
Stringํ˜•์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ํ•จ์ˆ˜๋“ค์ด ๋งŽ์€๋ฐ, ๋Œ€ํ‘œ์ ์œผ๋กœ ์•Œ์•„์„œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
toUpperCase, toLowerCase

let name = 'Dev Lee'
let upperLastName = name.toUpperCase();
let lowerLastName = name.toLowerCase();

console.log(name);  // 'Dev Lee'
console.log(upperLastName);  // 'DEV LEE'
console.log(lowerLastName);  // 'dev lee'

โฉ String ๋ฌธ์ž๊ธธ์ด ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
Stringํ˜•๋„ length๋ผ๋Š” ์†์„ฑ์œผ๋กœ ๊ธธ์ด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค(=๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ .length ์†์„ฑ์œผ๋กœ ์•Œ๋“ฏ)
(๋ณ€์ˆ˜).length

โ—พ ํšŒ์›๊ฐ€์ž…์„ ๋ฐ›์„ ๋•Œ ์ด๋ฆ„์— 10์ž๊ฐ€ ๋„˜์–ด๊ฐ”๋Š”์ง€ ์ฒดํฌํ•˜๊ฑฐ๋‚˜, ํ•œ๊ตญ์˜ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ๋Š” ๋ฌด์กฐ๊ฑด 10์ž ์•„๋‹ˆ๋ฉด 11์ž ์ด๋ฏ€๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ’์„ ์ž˜๋ชป ์ž…๋ ฅํ–ˆ๋Š”์ง€ ์ฒดํฌํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ.

// ์‚ฌ์šฉ์ž๊ฐ€ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์ž…๋ ฅํ•  ๋•Œ ์˜ˆ์‹œ(์ž…๋ ฅ ์ˆซ์ž ์ž๋ฆฟ์ˆ˜๋กœ ํ™•์ธ)
if (phoneNumber.length !== 10 &&  phoneNumber.length !== 11) {
    alert("ํฐ ๋ฒˆํ˜ธ ์ œ๋Œ€๋กœ ์ž…๋ ฅํ•˜์…จ๋‚˜์š”?");
}

// ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฆ„ ์ž…๋ ฅํ•  ๋•Œ ์˜ˆ์‹œ(์ž…๋ ฅ ๊ธ€์ž ์ž๋ฆฟ์ˆ˜๋กœ ํ™•์ธ)
function yourName(name) {
    if (name.length === 2) {
        return("์™ธ์ž์ด์‹œ๊ตฐ์š”! ์ด๋ฆ„์ด ์ด์˜๋„ค์š”.");
    } 
    if (name.length === 1) {
       return("์ œ๋Œ€๋กœ ์ž…๋ ฅํ•˜์…จ๋‚˜์š”?");
    } 
    if (name.length === 3) {
      return("๋ฉ‹์ง„ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.");
    }
}
console.log(yourName("์ˆ˜๋ จ"));  //  "์™ธ์ž์ด์‹œ๊ตฐ์š”! ์ด๋ฆ„์ด ์ด์˜๋„ค์š”." ์ถœ๋ ฅ

โฉ String ๋ฌธ์ž์—ด ์ฐพ๊ธฐ
indexOf() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์— ํŠน์ • ๋ฌธ์ž์—ด์ด ๋“ค์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๋งŒ์•ฝ ์žˆ๋‹ค๋ฉด ๋ช‡๋ฒˆ ์งธ ์ˆœ์„œ์— ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.
(๋ณ€์ˆ˜).indexOf("ํŠน์ • ๋ฌธ์ž์—ด")

โ— ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โ—พ ๋Œ“๊ธ€์— ์š•์„ค์ด ํฌํ•จ๋˜๋ฉด ๋‹ฌ์ง€ ๋ชปํ•˜๋„๋ก ์ฐจ๋‹จํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฐจ๋‹จํ•  ๋‹จ์–ด ๋ชจ์Œ์ด ์กด์žฌํ•œ๋‹ค๊ณ  ํ•˜๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ๋Œ“๊ธ€์„ ์ž…๋ ฅํ•œ ๋’ค "๋Œ“๊ธ€๋‹ฌ๊ธฐ"๋ฅผ ๋ˆ„๋ฅผ ๋•Œ, ์ฐจ๋‹จํ•  ๋‹จ์–ด ๋ชจ์Œ ์ค‘์— ํ•˜๋‚˜๋ผ๋„ ๋งค์น˜๋˜๋ฉด ๋Œ“๊ธ€์„ ๋‹ฌ์ง€ ๋ชปํ•˜๋„๋ก ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.

let info = "JavaScript๋Š” ํ”„๋กœ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค.";
let firstChar = info.indexOf("ํ”„๋กœ๋ž˜๋ฐ");  //"ํ”„๋กœ๋ž˜๋ฐ" ๋ฌธ์ž์—ด์ด ๋ช‡๋ฒˆ์งธ์— ์žˆ๋Š”์ง€ ์ฐพ๋Š” ํ•จ์ˆ˜๋ฅผ firstChar ๋ณ€์ˆ˜์— ๋‹ด์•˜๋‹ค.

console.log(info, firstChar);  // 'JavaScript๋Š” ํ”„๋กœ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค.', 12 ์ถœ๋ ฅ

if (firstChar !== -1) {    // "ํ”„๋กœ๋ž˜๋ฐ" ๋ฌธ์ž์—ด์ด ์กด์žฌํ•œ๋‹ค๋ฉด (-1์€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป์ด๋‹ˆ๊น)
                           // true. "ํ”„๋กœ๋ž˜๋ฐ"์ด๋ผ๋Š” ์˜คํƒ€๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ์œผ๋ฉด if๋ฌธ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    info = info.slice(0, firstChar) + "ํ”„๋กœ๊ทธ๋ž˜๋ฐ" + info.slice(firstChar+4, info.length); 
}
// slice๋Š” ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ…์ŠคํŠธ๋ฅผ ์ž˜๋ผ์ฃผ๋Š” ํ•จ์ˆ˜
// slice(์ž˜๋ฆด ์‹œ์ž‘์œ„์น˜, ์ž˜๋ฆด ๋์œ„์น˜)

// info.slice(0, firstChar) โžก ๋ฌธ๊ตฌ์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’(0)๋ถ€ํ„ฐ "ํ”„"์ „๊นŒ์ง€(12) ์ž˜๋ผ์ค€๋‹ค
// + "ํ”„๋กœ๊ทธ๋ž˜๋ฐ" + โžก "ํ”„๋กœ๊ทธ๋ž˜๋ฐ" ๋‹จ์–ด ์‚ฝ์ž…
// info.slice(firstChar+4, info.length) โžก  firstChar๊ฐ€ "ํ”„"๋กœ ์‹œ์ž‘ํ•˜๊ณ  "ํ”„๋กœ๋ž˜๋ฐ"์€ 4๊ธ€์ž ์ด๋‹ˆ๊น 'firstChar+4' = "์–ธ" ๊ธ€์ž ์ง์ „๊นŒ์ง€ ๋ถ€๋ถ„์—์„œ ์ž๋ฅด๊ธฐ ์‹œ์ž‘, ์ž˜๋ฆด ๋์œ„์น˜๋Š” info.length ์ด๋‹ˆ๊น ๋ฌธ์žฅ์˜ ๋งˆ์ง€๋ง‰๊นŒ์ง€.
// ๋”ฐ๋ผ์„œ ๋‚จ๊ธธ ๋ฌธ์ž๋“ค๋งŒ ์ž˜๋ผ์ฃผ๋Š” ๊ฒƒ. "ํ”„๋กœ๋ž˜๋ฐ" ๋ฌธ์ž์˜ ์ „ํ›„๋ฅผ ๋ชจ๋‘ slice ํ•˜๊ณ ์žˆ๋‹ค.

console.log(info);  // info๋Š” slice๋กœ ์ž˜๋ผ๋‚ธ ๋‚˜๋จธ์ง€์™€ "ํ”„๋กœ๊ทธ๋ž˜๋ฐ"์ด๋ผ๋Š” ๋‹จ์–ด ์‚ฝ์ž…์œผ๋กœ ์žฌ๊ตฌ์„ฑํ–ˆ๋‹ค

๐Ÿš€ slice ํ•จ์ˆ˜

๐Ÿ”ธ Array, String ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ slice๋Š” ๋ฐฐ์—ด(๋ฌธ์ž์—ด)์˜ ์ผ๋ถ€๋ถ„(slice) ํ˜น์€ ๋ถ€๋ถ„ ๋ฐฐ์—ด(subarray)์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๐Ÿ”ธ slice() ๋ฉ”์„œ๋“œ๋Š” ์ „๋‹ฌ ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ๋ฐ›๋Š”๋ฐ, ๊ฐ ์ธ์ž๋Š” ๋ฐ˜ํ™˜๋  ๋ถ€๋ถ„์˜ ์ฒ˜์Œ๊ณผ ๋์„ ๊ฐ๊ฐ ๋ช…์‹œํ•œ๋‹ค.
๐Ÿ‘‰ slice(์ž˜๋ฆด ์‹œ์ž‘์œ„์น˜, ์ž˜๋ฆด ๋์œ„์น˜)
๐Ÿ”ธ ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐฐ์—ด์€ ์ฒซ ๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž๊ฐ€ ์ง€์ •ํ•˜๋Š” ์œ„์น˜๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž๊ฐ€ ์ง€์ •ํ•˜๋Š” ์œ„์น˜๋ฅผ ์ œ์™ธํ•œ ๊ทธ ์‚ฌ์ด์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ํฌํ•จํ•œ๋‹ค.
๐Ÿ”ธ ๋งŒ์•ฝ ์ „๋‹ฌ ์ธ์ž๋ฅผ ํ•˜๋‚˜๋งŒ ๋ช…์‹œํ•˜๋ฉด, ๊ทธ ์œ„์น˜์—์„œ ๋ฐฐ์—ด ๋๊นŒ์ง€์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๐Ÿ”ธ ๋งŒ์•ฝ ์ „๋‹ฌ ์ธ์ž๊ฐ€ ์Œ์ˆ˜๋ผ๋ฉด ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ์—์„œ ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋กœ ๋ฐฐ์—ด ์›์†Œ๋ฅผ ์ง€์ •ํ•œ๋‹ค.
ex) ์ „๋‹ฌ ์ธ์ž -1์€ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, ์ „๋‹ฌ์ธ์ž -3์€ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ถ€ํ„ฐ ์•ž์ชฝ์œผ๋กœ ์„ธ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

/* 
sliceCityFromAddress ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ์ฃผ์„ธ์š”.
sliceCityFromAddress ํ•จ์ˆ˜๋Š” address ์ธ์ž๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.
address ์ธ์ž์—๋Š” ์ฃผ์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” string์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
์ฃผ์–ด์ง„ ์ฃผ์†Œ๊ฐ€ ์–ด๋Š ๋„์‹œ์ธ์ง€๋ฅผ ์ฐพ์•„๋‚ด์„œ, ํ•ด๋‹น ์ฃผ์†Œ์—์„œ ๋„์‹œ ๋ถ€๋ถ„๋งŒ ์‚ญ์ œํ•œ ์ƒˆ๋กœ์šด ์ฃผ์†Œ๋ฅผ ๋ฆฌํ„ดํ•ด ์ฃผ์„ธ์š”.
์ฃผ์†Œ๋Š” ๋ฌด์กฐ๊ฑด "์‹œ" ๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "์„œ์šธ์‹œ".
"๋„" ์™€ "์‹œ" ๋Š” ์ฃผ์†Œ์— ํ•œ๋ฒˆ ๋ฐ–์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฃผ์†Œ๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๋ฉด,
"๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ ๋ถ„๋‹น๊ตฌ ์ค‘์•™๊ณต์›๋กœ 53"
๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ’์ด ๋ฆฌํ„ด๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:
"๊ฒฝ๊ธฐ๋„ ๋ถ„๋‹น๊ตฌ ์ค‘์•™๊ณต์›๋กœ 53"
*/

function sliceCityFromAddress(address){
    let info = address;   // ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‹ด๋Š” ๋ณ€์ˆ˜ ์ƒ์„ฑ(ํ•จ์ˆ˜ ๋‚ด ํ™œ์šฉ)
    let firstChar = info.indexOf("๋„");  // "๋„"๊ฐ€ ๋ช‡๋ฒˆ์งธ์— ์œ„์น˜ํ•˜๋Š”์ง€ ๋‹ด๋Š” ๋ณ€์ˆ˜ ์ƒ์„ฑ, ๋„ ์•ž๊นŒ์ง€ 
    let secondChar = info.indexOf("์‹œ");  // "์‹œ"๊ฐ€ ๋ช‡๋ฒˆ์งธ์— ์œ„์น˜ํ•˜๋Š”์ง€ ๋‹ด๋Š” ๋ณ€์ˆ˜ ์ƒ์„ฑ, ์‹œ ์•ž๊นŒ์ง€ 
   
    if (firstChar !== -1) {   // "๋„"๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด(= -1์€ ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ, -1์„ ๋ถ€์ •ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •)
      info = info.slice(0, firstChar + 1) + info.slice(firstChar + 5, info.length); 
    } 
    // ๋งจ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž์—ด(0)๋ถ€ํ„ฐ ~ "๋„" ๊นŒ์ง€(firstChar+1) ๋‚จ๊ธฐ๊ณ ,
    // "๋„" ์ดํ›„ 5๊ธ€์ž(๋„โœ”์„ฑ๋‚จ์‹œ)๋ถ€ํ„ฐ ~ ๋ฌธ์ž์—ด ์ „์ฒด(info.length) ๋‚จ๊ธฐ๊ณ ,
    // ์ž˜๋ฆฐ ๋ถ€๋ถ„์€ firstChar +1 ~ firstChar +5 ์‚ฌ์ด์˜ ๋ฌธ์ž์—ด์ด๋‹ค (= โœ”์„ฑ๋‚จ์‹œโœ”) 
    else if (secondChar !== -1){
      info = info.slice(0, secondChar - 4) + info.slice(secondChar + 2, info.length);
    }
    // ๋งจ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž์—ด(0)๋ถ€ํ„ฐ ~ "์‹œ" ์•ž์˜ 4๊ธ€์ž ๊นŒ์ง€(secondChar-4) ๋‚จ๊ธฐ๊ณ , (= secondChar-4๊ฐ€ ์œ„์น˜ํ•œ ์ง€์ ์€ "์‹œ" ์•ž์„ ๊ธฐ์ค€์œผ๋กœ -4๊ธ€์ž ํ•œ ์ง€์ ์ด๋‹ค 
    // = ๐Ÿšฉ์„œใ…ฃ์šธใ…ฃํŠนใ…ฃ๋ณ„ใ…ฃ"์‹œ" (0๋ถ€ํ„ฐ ๐Ÿšฉ์ง€์ ๊นŒ์ง€ ๋‚จ๊ธฐ๋Š” ๊ฒƒ)
    // "์‹œ" ํฌํ•จํ•˜์—ฌ ์ดํ›„ 2๊ธ€์ž(= ์‹œโœ”)์ดํ›„ ๋ถ€ํ„ฐ ~ ๋ฌธ์ž์—ด ์ „์ฒด(info.length) ๋‚จ๊ธฐ๊ณ ,
    // ์ž˜๋ฆฐ ๋ถ€๋ถ„์€ secondChar -4 ~ secondChar + 2 ์‚ฌ์ด์˜ ๋ฌธ์ž์—ด์ด๋‹ค (= ์„œ์šธํŠน๋ณ„"์‹œโœ”) 
    return info;
}
console.log(sliceCityFromAddress("๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ ์ค‘์•™๊ณต์›๋กœ 53"));   //'๊ฒฝ๊ธฐ๋„ ์ค‘์•™๊ณต์›๋กœ 53'
console.log(sliceCityFromAddress("์„œ์šธํŠน๋ณ„์‹œ ๊ฐ•๋‚จ๊ตฌ ํ…Œํ—ค๋ž€๋กœ 427 ์œ„์›Œํฌํƒ€์›Œ"));  //'๊ฐ•๋‚จ๊ตฌ ํ…Œํ—ค๋ž€๋กœ 427 ์œ„์›Œํฌํƒ€์›Œ'
console.log(sliceCityFromAddress("๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ ๋ถ„๋‹น๊ตฌ ํŒ๊ต์—ญ๋กœ 235"));  //'๊ฒฝ๊ธฐ๋„ ๋ถ„๋‹น๊ตฌ ํŒ๊ต์—ญ๋กœ 235'

๐Ÿง String ํ•จ์ˆ˜ ์ฐธ๊ณ  site
https://www.w3schools.com/jsref/jsref_obj_string.asp

๐Ÿ‘‰ ๋ฌธ์ž์—ด + ๋ฌธ์ž์—ด/์ˆซ์ž ์กฐํ•ฉ
โฉ ๋ฌธ์ž์—ด+๋ฌธ์ž์—ด

  • ํ…์ŠคํŠธ๋ฅผ ์กฐํ•ฉํ•  ๋•Œ๋Š” + ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ
  • ํ…์ŠคํŠธ์™€ ๋ณ€์ˆ˜๋ฅผ ์กฐํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค
console.log("์•ˆ๋…•" + "ํ•˜์„ธ" + "" + "์š”");

let hi = "์•ˆ๋…•";
console.log(hi + "ํ•˜์„ธ์š”");

let ha = "ํ•˜์„ธ์š”";
console.log(hi + ha);

/* (์ฐธ๊ณ ) ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ 
ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ๋Š” ํ•ญ์ƒ 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€๋งŒ, 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ˆซ์ž ํ‘œํ˜„์‹์€ ์—†๋‹ค.
๊ทธ๋ž˜์„œ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ๋Š” ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์ง€๋งŒ, ๋ฌธ์ž์—ด๋กœ ๊ด€๋ฆฌํ•ด์•ผ ์ปดํ“จํ„ฐ๊ฐ€ ์ฒซ ์ˆซ์ž์ธ "0"๋„ ๊ฐ–๊ณ  ์žˆ๊ฒŒ ๋œ๋‹ค.
*/
let phoneNumber = 01012349876;

console.log(phoneNumber);  //1012349876
console.log(phoneNumber.length);   //undefined
// ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ๋Š” ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•ด์ค˜์•ผ ํ•œ๋‹ค
let phoneNumber = "01012349876";

console.log(phoneNumber);  //01012349876
console.log(phoneNumber.length);   //11

โฉ ๋ฌธ์ž์—ด+์ˆซ์ž
โ— String๊ณผ Numberํ˜•์„ ๋”ํ•˜๋ฉด ํ•ญ์ƒ String ํ˜•์œผ๋กœ ๋ณ€ํ™˜

console.log("2" + "2");  // 22์ถœ๋ ฅ(๋ฌธ์ž์—ด๋กœ ์ธ์‹)
console.log(2 + 2);      // ์ˆซ์ž 4

โฉ String <-> Number ๋ณ€ํ™˜
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์˜ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
typeof ์—ฐ์‚ฐ์ž๋กœ ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ ๋ฌด์Šจ type์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

let a = '900';  
let b = 350; 
let c = Math.random(); 
let d = '5' + 5;   // ๋ฌธ์ž+์ˆซ์ž = ๋ฌธ์ž

console.log(typeof a);  //'string'
console.log(typeof b);  //'number'
console.log(typeof c);  //'number'
console.log(typeof d);  //'string'


console.log('200'+'10');  //'20010' (๋ฌธ์ž์—ด +์—ฐ์‚ฐ)
console.log('200'-'10');  //190 (-๋Š” ๋ฌด์กฐ๊ฑด ์ˆซ์ž์—ฐ์‚ฐ โžก ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์ง„ ๋ฌธ์ž์—ด์ผ์ง€๋ผ๋„ ์ˆ˜ํ•™์—ฐ์‚ฐ ์ฒ˜๋ฆฌ๋จ)
console.log(200+'11');    //'20011' (๋ฌธ์ž์—ด+์ˆซ์ž์—ด = ๋ฌธ์ž์—ด)
console.log('200'+11);    //'20011' (๋ฌธ์ž์—ด+์ˆซ์ž์—ด = ๋ฌธ์ž์—ด)
console.log('200'-'์•ˆ๋…•'); //NaN
โ— ์ˆซ์ž ์—ฐ์‚ฐ์—๋งŒ ์‚ฌ์šฉ๋˜๋Š” -์—ฐ์‚ฐ์ž๋ฅผ ๋ฌธ์ž์— ๋„ฃ์–ด์„œ Not a Number๊ฐ€ ์ถœ๋ ฅ


function join(){
  let birthYear = "2000";
  let currentYear = 2019;
  return currentYear-birthYear;
}
join();   //19 (๋ฌธ์ž์—ด๊ณผ ์ˆซ์ž์—ด์ธ๋ฐ -์—ฐ์‚ฐ์ž ์‚ฌ์šฉ์œผ๋กœ ์ธํ•ด ์—ฐ์‚ฐ์ฒ˜๋ฆฌ ๋จ. ๋บ„์…ˆ์˜ ๊ฒฐ๊ณผ ์ถœ๋ ฅ)


console.log(2019+"2000");  // '20192000' ์ถœ๋ ฅ 
// +์—ฐ์‚ฐ์ž๋กœ ์ธํ•ด ๋ฌธ์ž์—ด+์ˆซ์ž์—ด=๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
console.log(2019-"2000");  // 19 ์ถœ๋ ฅ
// -์—ฐ์‚ฐ์ž๋Š” ์ˆซ์ž์—๋งŒ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์–‘์ชฝ ๋ชจ๋‘ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๊ณ„์‚ฐํ•ด์คŒ

โ— ํ…์ŠคํŠธ๋กœ ํ‘œํ˜„๋œ ์ˆซ์ž๋Š” ๊ผญ ์ˆซ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๊ณ„์‚ฐํ•ด์ฃผ๊ธฐ
Number(์ •์ˆ˜,์†Œ์ˆ˜), parseInt(์ •์ˆ˜), parseFloat(์†Œ์ˆ˜)

parseInt("1.901"); โžก 1 ์ถœ๋ ฅ, `์ •์ˆ˜ ๋ฐ˜ํ™˜`
parseFloat("1.901"); โžก 1.901 ์ถœ๋ ฅ, `์†Œ์ˆ˜ ๋ฐ˜ํ™˜`
Number("1.901"); โžก 1.901 ์ถœ๋ ฅ, `์ •์ˆ˜ ์†Œ์ˆ˜ ๋ชจ๋‘ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ`
parseInt("200") + 1; โžก 201 ์ถœ๋ ฅ

โ— ์—ฐ์‚ฐ์˜ ํŠน์„ฑ์„ ํ™œ์šฉํ•ด์„œ String ํ˜•์„ Numberํ˜•์œผ๋กœ ๋ณ€ํ™˜
-์—ฐ์‚ฐ์ž ์ด์šฉ

var numberAsNumber = "1234"; 
var numberAsString = numberAsNumber - 0;

console.log(numberAsNumber, typeof numberAsNumber);
// '1234' 'string' ์ถœ๋ ฅ
console.log(numberAsString, typeof numberAsString);
// 1234 'number' ์ถœ๋ ฅ ( - ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์ˆซ์ž์—ฐ์‚ฐ์ด ๋จ)

โ— Numberํ˜•์—์„œ Stringํ˜•์œผ๋กœ ๋ณ€ํ™˜ ํ•˜๋Š” ๊ฒฝ์šฐ
.toString, (์ˆซ์ž)+""

var numberAsNumber = 1234; 
var numberAsString = numberAsNumber.toString();
var numberAsString = 1234 + "";

console.log(numberAsNumber, typeof numberAsNumber);
// 1234 'number' ์ถœ๋ ฅ
console.log(numberAsString, typeof numberAsString);
// '1234' 'string' ์ถœ๋ ฅ
console.log(numberAsString, typeof numberAsString);
// '1234' 'string' ์ถœ๋ ฅ

๐Ÿ‘‰ boolean

  • boolean ์€ ๋‹จ ๋‘ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ ํƒ€์ž… โ†’ true / false
  • boolean ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ.
  • ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด boolean ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

โฉ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ boolean ๊ฐ’ ๋ณ€ํ™˜

โœ”๏ธ true ๋กœ ๋ณ€ํ™˜๋˜๋Š” ๊ฐ’

  • ๋ฌธ์ž์—ด : ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๋ชจ๋“  ๋ฌธ์ž์—ด
  • ์ˆซ์ž : 0 ์ด ์•„๋‹Œ ๋ชจ๋“  ์ˆซ์ž
  • ๊ฐ์ฒด: ๋ชจ๋“  ๊ฐ์ฒด ({ }, [ ] ํฌํ•จ)

โœ”๏ธ false ๋กœ ๋ณ€ํ™˜๋˜๋Š” ๊ฐ’ ๐Ÿšจ๐Ÿšจ๐Ÿšจ

  • ๋ฌธ์ž์—ด : " " (๋นˆ๋ฌธ์ž์—ด)
  • ์ˆซ์ž : 0, NaN
  • ๊ฐ์ฒด : null
  • undefined

โฉ ์กฐ๊ฑด๋ฌธ์—์„œ์˜ boolean ๋ฐ์ดํ„ฐ ๊ฐ’ ํ™œ์šฉ

let num = 3;
if (num === 3) {  //num === 3 ์ด true์ด๋ฉด
  // ์‹คํ–‰
}

let msg = "message";
if (msg) {   // ๋ฌธ์ž์—ด์€ " "(๋นˆ ๋ฌธ์ž์—ด)์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ true ๊ฐ’์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ
  // ์‹คํ–‰
}

let number = 0;
if (number) {   // 0์ด ์•„๋‹Œ ๋ชจ๋“  ์ˆซ์ž๋งŒ true๋กœ ๋ณ€ํ™˜
  // ์‹คํ–‰ ์•ˆ๋จ
}

๐Ÿ‘‰ undefined

  • undefined ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ์ •์˜๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.
  • let, const ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์ดˆ๊ธฐํ™” ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ์ฆ‰ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ๋งŒ ํ•˜๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ณ€์ˆ˜์—๋Š” undefined ๊ฐ€ ํ• ๋‹น๋œ๋‹ค.
let msg;
console.log(msg === undefined); //true (msg๋Š” ์„ ์–ธ๋งŒ ํ•˜๊ณ , ์ดˆ๊ธฐํ™” ํ•˜์ง€ ์•Š์•˜๋‹ค.)

๐Ÿ‘‰ ๊ฐ์ฒด(Object)

  • ๊ฐ์ฒด๋Š” ํ‚ค(Key)์™€ ๊ฐ’(Value)์˜ ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ.
  • ์ค‘๊ด„ํ˜ธ({ })๋กœ ๊ฐ์‹ธ์ง„ key-value(property)์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ–๋Š”๋‹ค.
const person = {
	name : "leedev",
	age : 18,
	skills : ['HTML', 'CSS', 'Javascript'] 
}

๐Ÿง ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜(์ฝ”๋“œ์ž‘์„ฑ๊ฐ€์ด๋“œ) ์ฐธ๊ณ  site
https://github.com/airbnb/javascript

profile
boma91@gmail.com

0๊ฐœ์˜ ๋Œ“๊ธ€