August 25, 2022
'a'.charCodeAt(0); // 아스키코드 출력
String.fromCharCode(65, 66); // 'AB'
String.fromCharCode(65); // 'A'str.includes('dog'); // 문자열을 포함하고 있는지
const regex = RegExp('dog', 'g');
if (regex.exec(str) !== null) {
  // 문자열을 포함하고 있는지
}
console.log((str.match(/dog/g) || []).length); // 일치하는 문자열이 몇 개인지const str = 'hello';
console.log(str.slice(0, 2)); // 'he'
console.log(str.slice(1)); // 'ello'// 문자열 내에 값 찾아서 바꾸기
str = str.replace(/dog/gi, 'cat');
str = str.replaceAll(/dog/gi, 'cat'); // ES2021/ES12, Nodejs v15부터 지원10 + ''; // number → string, 추가하기1000.toLocaleString(); // 1,000 if in U.S. English locale'abc'.repeat(2); // 'abcabc'
'abc'.repeat(0); // ''let str = str.toLowerCase();
str = str.toUpperCase();if (str.startsWith('start')) {
  // 시작한다면
}
if (str.endsWith('end')) {
  // 끝난다면
}
// 시작과 끝 채우기
let str = '11';
console.log(str.padStart(4, '0')); // 0011
console.log(str.padEnd(4, '0')); // 1100const result = Object.assign({ id: 1 }, { name: 'Lee' });const names = ['Lee', 'Kim'];
console.log(names.indexOf('Lee')); // 0
console.log(names.indexOf('Park')); // -1arr.splice(5, 1); // 5번째 인덱스 값 제거[...new Array(n)].forEach(() => {});const arr1 = Array.from(Array(10).keys()); // 0부터 9까지 채우기
const arr2 = new Array(10).fill(false); // 10개를 false로 채우기
const arr3 = Array.from({ length: 5 }, (_, index) => index); // [0, 1, 2, 3, 4]// N*M 크기의 이차원 배열 만들고 false로 채우기
const visited = Array.from(Array(N), () => Array(M).fill(false));
const visited = [...Array(N)].map(e => Array(M).fill(false));arr = [];
arr.length = 0;
arr = arr.splice(0, arr.length);arr.reverse();const arr = arr1.concat(arr2);if (arr.includes('dog')) {
} // trueif (
  userNumberList.some((number, index, array) => array.indexOf(number) !== index)
) {
  // true면 배열 내부에서 겹치는 값이 존재하는 것
}// Array → String
const arr = ['h', 'e', 'l', 'l', 'o'];
arr.toString(); // h,e,l,l,o
arr.join(''); // hello
arr.join('+'); // h+e+l+l+oarr.sort(function(a, b) {
  if (a.city === b.city) {
    return b.price - a.price;
  }
  return a.city > b.city ? 1 : -1;
});
// 문자열 정렬
arr.sort((a, b) => a.localeCompare(b));const result = arr.reduce((prev, cur) => [...prev, cur.cost], []);[1, 2] === [1, 2]는 false이므로 Set을 통해 object를 저장하면 object가 duplicated될 수 있다
const nums = [3, 1, 2, 3];
const mySet = [...new Set(nums)]; // array → set
const myArr = [...mySet]; // set → array
const mySet = new Set();
mySet.add(1); // Set { 1 }
mySet.has(1); // true
mySet.size;
mySet.delete(1);const myMap = new Map();
myMap.set('id', 0);
myMap.get('id');
myMap.delete('id');
myMap.clear();
console.log(myMap.size);
if (myMap.has('id')) {
}
for (const [key, value] of myMap) {
}
myMap.forEach((value, key, map) => {});const myMap = new Map();
myMap
  .set('R', 0)
  .set('T', 0) // 방법1
  [('R', 'T')].forEach(el => myMap.set(el, 0)); // 방법2
const myMap2 = new Map([
  ['a', 1],
  ['b', 2],
]); // 방법3const stack = [];
stack.push(1); // 배열 맨 뒤에 값 삽입
stack.pop(); // 배열 맨 마지막 값 제거
const queue = [];
queue.shift(); // 배열 맨 앞 값 제거
arr.unshift(1); // 배열 맨 앞에 값 삽입numberArr.sort(function(a, b) {
  return a - b;
});
numberArr.sort((a, b) => a - b);num.toString(2);isNaN(num); // number면 false 리턴
isNaN('1e10000'); // false (This translates to Infinity, which is a number)
isNaN(''); // false
isNaN(' '); // false
isNaN(false); // false
Number.isInteger(Number(1));
if (num % 1 === 0) {
}arr.map(Number);
arr.map(el => +el);Math.max(1, 3, 4); // 여러 개 가능
Math.min(1, 3, 4);
const arr = [1, 3, 2]; // array인 경우는 아래처럼 구하기
console.log(Math.max(...arr));
Number.MAX_SAFE_INTEGER;
Number.MIN_SAFE_INTEGER;3 / 2; // 1.5
Math.floor(3 / 2); // 버림
Math.ceil(3 / 2); // 올림
Math.round(3 / 2); // 반올림
Math.pow(2, 3); // 2의 3제곱
2 ** 3; // 2의 3제곱 (ES7에 도입)
Math.abs(); // 절대값
console.log((23.345).toFixed(2)); // "23.34"let sum = arr.reduce((prev, cur) => prev + cur, 0);