Javascript 에는 두가지 종류의 변수타입이 있다.
(MDN 참고 https://developer.mozilla.org/ko/docs/Glossary/Primitive)
primitive types 예제
let a = 13 // assign '13' to 'a'
let b = a // copy the value of 'a' to 'b'
b = 37 // assign '37' to 'b'
console.log(a) // ==> 13
reference types 예제
let a = {c:13} // assign the reference of new object to 'a'
let b = a // copy the reference of the object inside 'a' to new variable 'b'
b.c = 37 // modify the contents of the object 'b' refers to
console.log(a) // ==> {C:37}
문자열string은 텍스트 형태로 표현될 수 있는 데이터를 보관하는데 유용하다.
문자열에서 개개의 문자에 접근하는 방법은 두가지가 있다.
(MDN 참고 https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String)
a. charAt( ) methods
return 'cat'.charAt(1); // returns "a"
b. str[index]
return 'cat'[1]; // returns "a"
✦ index로 개개인의 문자에 접근은 가능하지만 속성을 바꾸거나 쓸 수는 없다. (read only)
CONCATENATING STRINGS
str.length
var str = 'CodeStates' ;
console.log(str.length) ; // 10
str.indexOf
'Blue Whale'.indexOf('Blue') ; // 0 ('Blue'라는 문자열이 앞에서 0번째부터 나오기 때문)
'Blue Whale'.indexOf('blue') ; // -1 (찾고자 하는 문자열이 없기 때문)
'Blue Whale'.indexOf('Whale') ; // 5 ('Whale'이라는 문자열이 앞에서 5번째부터 나오기 때문)
'Blue Whale Whale'.indexOf('Whale') ; // 5 ('Whale'이라는 문자열이 5번째부터 나오기 때문)
'canal'.lastIndexOf('a') ; // 3 ('a'라는 문자열이 뒤에서 3번째부터 나오기 때문)
str.includes(searchValue)
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be')); // true
console.log(str.includes('question')); // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1)); // false
console.log(str.includes('TO BE')); // false
str.split(seperator)
var str = '대표님 퇴근 좀 빨리 시켜주세요' ;
console.log(str.split(' '));
// ['대표님', '퇴근', '좀', '빨리', '시켜주세요']
str.substring(start, end)
var str = 'abcdefghij';
console.log(str.substring(0, 3)) ; // 'abc'
console.log(str.substring(3, 0)) ; // 'abc' (시작start 과 끝end 순서가 달라도 상관 없다. 작은 수가 start, 큰 수가 end)
console.log(str.substring(1, 4)) ; // 'bcd'
console.log(str.substring(-1, 4)) ; // 'abcd' (음수는 0번째로 취급한다)
console.log(str.substring(0, 3)) ; // 'abcdefghij' (index 범위가 넘을 경우 마지막 번째 index로 취급)
str.slice
{
const array = [1, 2, 3, 4, 5];
const result = array.slice(0, 2);
console.log(result);
// (2) [1, 2]
console.log(array);
// (5) [1, 2, 3, 4, 5]
// array는 변하지 않음.
}
str.toLowerCase( ) / str.toUpperCase( ) : IMMUTABLE
var sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toLowerCase());
// expected output: "the quick brown fox jumps over the lazy dog."
console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
str.trim( )
var orig = ' foo ';
console.log(orig.trim()); // 'foo'
// 한 쪽의 공백만 제거하는 .trim() 예제
var orig = 'foo ';
console.log(orig.trim()); // 'foo'
str.match(advanced)
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /[A-Z]/g;
var found = paragraph.match(regex);
console.log(found);
// expected output: Array ["T", "I"]
str.replace(advanced)
var p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
var regex = /dog/gi;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"