๐Ÿ˜ถ B.1 ๊ธฐ๋ณธ ์‚ฌ์šฉ (Basic Usages)

String, ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์€ Javascript์˜ ๊ธฐ๋ณธํƒ€์ž… (primitive data type) ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๋ฌธ์ž์—ด์€ ๋ณ€์ˆ˜์— ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๋˜๋Š” ํ…์ŠคํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ์‚ฌ์šฉ๋œ๋‹ค.

Javascript์—์„œ ๋ฌธ์ž์—ด์€ ' ' ์†Œ๋”ฐ์˜ดํ‘œ ๋˜๋Š” " " ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ผ ํ…์ŠคํŠธ(text) ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

    var a = 'my first String!'
    console.log(a)
  // output>    my first String!

    var b = "my first String!"
    console.log(b)
  // output>    my first String!

๋ฌธ์ž์—ด ๋‚ด์—์„œ ๋‘˜๋Ÿฌ์‹ผ ๋”ฐ์˜ดํ‘œ์™€ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    var a = 'my first "String!"'
    console.log(a)
  // output>    my first "String!"
    var b = "my first 'String!'"
    console.log(b)
  // output>    my first 'String!'


๐Ÿ˜ƒ ๋ฌธ์ž์—ด ์ƒ์„ฑ

๋ณ€์ˆ˜(variable)์˜ ๊ฐ’์œผ๋กœ ์ง€์ •

๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด์€ ๋ฆฌํ„ฐ๋Ÿด๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์˜ ๊ฐ’์œผ๋กœ ์ง€์ •ํ•œ๋‹ค. ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๊ธฐ๋ณธ์ ์œผ๋กœ
๐Ÿฅ‡ var
๐Ÿฅˆ let
๐Ÿฅ‰ const

๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹์˜ ์ฐจ์ด๋Š” ๋‹ค๋ฅธ ํฌ์ŠคํŠธ์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    var a = 'my'
    let b = 'first'
    const c = '"String!"'
    console.log(a+b+c)
  // output>    my first "String!"

๊ฐ์ฒด(Object)๋กœ ์ƒ์„ฑ

ํ†ต์ƒ์ ์œผ๋กœ Javascript์˜ ๋ฌธ์ž์—ด์€ ๋ฆฌํ„ฐ๋Ÿด๋กœ ์ƒ์„ฑ๋œ ์›์‹œ์ ์ธ ๊ฐ’์ด๋‹ค (primitive values).
ํ•˜์ง€๋งŒ, ํ‚ค์›Œ๋“œ new ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด(object)๋กœ ์„ ์–ธ ํ•  ์ˆ˜ ์žˆ๋‹ค:

    var x = "John";
    var y = new String("John");
    console.log(typeof x);
    console.log(typeof y);
  // output>    string
  // output>    object    

๐Ÿง ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฐจ์ด๋ฅผ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค:

== : ๋Š์Šจํ•œ ๋น„๊ต๋Š” ๊ฐ™์€ ๊ฐ’์ด๋ฏ€๋กœ true ๋ฐ˜ํ™˜.
=== : ์—„๊ฒฉํ•œ ๋น„๊ต๋ฅผ ํ•œ๋‹ค๋ฉด ์ž๋ฃŒํ˜•์ด ๋‹ค๋ฆ„์œผ๋กœ false๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค.


๐Ÿ˜ฎ '+' / concat() ํ•จ์ˆ˜

๋ณ€์ˆ˜ ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์„ ์–ธ ํ›„ ์ดˆ๊ธฐํ™”์‹œ ๊ธฐ์กด ๋ผ์ธ์˜ ์ž๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜๋‹ค๋ฉด enterํ‚ค๋กœ ์ค„์„ ๊ฐœํ–‰ํ•˜์—ฌ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค:

    var strEnter =
    "Hello World!";
    var strBackslash = "Hello \
    World!";

    console.log(strEnter)
  // output>    Hello World!
    console.log(strBackslash)
  // output>    Hello World!

๐Ÿ™…โ€โ™‚๏ธ \ ๋ฐฑ์Šฌ๋ ˆ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์€ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ณดํŽธ์ ์œผ๋กœ ์ง€์›์„ ํ•˜์ง€ ์•Š์„์ˆ˜๋„ ์žˆ๋‹ค. ์ผ๋ถ€ ๋ธŒ๋ผ์šฐ์ €๋“ค์€ \ ๋ฌธ์ž๋’ค์— ๊ณต๋ฐฑ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ™†โ€โ™€๏ธ ์œ„ ์˜ˆ์ œ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์กฐ๊ธˆ ๋” ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ•์€ + ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค:

    var strPlus = "Hello"+"World!";    

    console.log(strPlus)
  // output>    Hello World!

๐Ÿ™†โ€โ™‚๏ธ concat() ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ๋ฐ›์€ ๋ชจ๋“  ๋ฌธ์ž์—ด๋“ค๊ณผ ํ˜ธ์ถœ๋œ ๋ฌธ์ž์—ด์„ ๋ถ™์—ฌ์„œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค:

    var str1 = 'Hello';
    var str2 = 'World';

    console.log(str1.concat(' ', str2));
  // output>    "Hello World"
    console.log(str2.concat(', ', str1));
  // output>    "World, Hello"

๐Ÿง concat()ํ•จ์ˆ˜๋Š” array ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•˜๋‚˜๋กœ ๋ฌถ์œผ๋ ค ํ•  ๋•Œ ์ƒ๋‹นํžˆ ์œ ์šฉํ•˜๋‹ค.


๐Ÿƒโ€โ™‚๏ธ B.2. Escape Characters

๋จผ์ € ์˜ˆ์ œ๋ฅผ ์‚ฌํŽด๋ณด์ž:

    var x = 'My 'name' is "Daniel" and I'm from \Argentina.';
  // output (ERROR)>    Unexpected identifier

๐Ÿ˜ฑ ์™œ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์„๊นŒ? ๋ฌผ๋ก , ์ผ์ฐจ์ ์œผ๋กœ ' '์†Œ ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ผ ๋ฌธ์ž์—ด ์•ˆ์— ๋˜ ๋‹ค์‹œ ์†Œ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด ๋ฌธ์ œ์ด๋‹ค. ํ•˜์ง€๋งŒ, ๋”ฐ์˜ดํ‘œ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค ํ•ด๋„ ์ด์™ธ์— ๋‹ค๋ฅธ ๋ฌธ์ œ๋“ค์„ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค. ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ๋ฌธ์ž์—ด์— ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ญ๊ฐ€ ํ•„์š”ํ• ๊นŒ?
๐Ÿคฉ ๋ฐ”๋กœ, \ ๋ฐฑ์Šฌ๋ž˜์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์ดescape์˜ ๊ฐœ๋…์ด๋‹ค.

Code Output Description
\' ' ์†Œ ๋”ฐ์˜ดํ‘œ
\" " ์Œ ๋”ฐ์˜ดํ‘œ
\\ \ ๋ฐฑ ์Šฌ๋ž˜์‹œ
    var a = 'My \'name\''
    let b = " is \"Daniel\""
    const c = ' and I\'m from \Argentina.'
    console.log(a+b+c)
  // output>    My 'name'is "Daniel"and I'm from Argentina.


๐Ÿค“ B.3. ์†์„ฑ (Properties)**

๐Ÿ“ ๋ฌธ์ž์—ด์˜ length

๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ž์˜ ์†์„ฑ์€ string์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. length๋Š” ์˜์–ด๋กœ ๊ธธ์ด๋ผ๋Š” ๋œป์œผ๋กœ ํ˜ธ์ถœ๋œ ๋ฌธ์ž์—ด์˜ ๊ธ€์ž์ˆ˜๋ฅผ ์ˆซ์ž(number ํƒ€์ž…) ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    console.log( alphabet.length );
  // output>    26

๐Ÿ”ข ๋ฌธ์ž์—ด์˜ N

N๋ฒˆ์งธ ์œ„์น˜์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. N์€ 0๊ณผ ๋ฌธ์ž์—ด์˜ length๋ณด๋‹ค ์ž‘์€ ๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š” ์ •์ˆ˜์ด๋‹ค. ๋‹ค๋ฅธ ๋ง๋กœ, N์€ ๋ฌธ์ž์—ด์˜ index์ธ๊ฒƒ์ด๋‹ค N ์†์„ฑ์€ ์ฝ๊ธฐ ์ „์šฉ(read-only)์˜ ์„ฑ๊ฒฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

    var abc = "abcdEfghijkLmnOpqrStUvwxyz";
    console.log(abc[18]+abc[4]+abc[14]+abc[20]+abc[11]);
  // output>    SEOUL






๐Ÿ‘จโ€๐Ÿ’ป ๊ฐœ์ธ์ ์ธ ํ•™์Šต ์šฉ๋„๋กœ ์ œ์ž‘ํ•œ post ์ž…๋‹ˆ๋‹ค. ์ธ์šฉํ•˜๋„ ํผ๊ฐ€์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์•„๋ž˜ ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿค™

post ๋‚ด์šฉ์— ์˜คํƒ€ ๋˜๋Š” ์ž˜๋ชป๋œ ์ง€์‹์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!