all methods return a new value -> do not change the original variables
String
toUpperCase();대문자로 변환.trim();공백 제거 복사str.indexOf('find')찾는 값의 위치를 찾음 = index number 반환str.slice(beginIndex[, endIndex])index 위치의 값을 반환str.replace(org, repl)
Number
수학 계산
Number.MAX_VALUE최대 정수값Number.MIN_VALUE최소 정수값Number.MAX_SAFE_INTEGER다룰 수 있는 최대 정수값Number.MIN_SAFE_INTEGER다룰 수 있는 최소 정수 값
어림 계산
Math.round(값)반올림Math.floor(값)버림Math.ceil(값)올림Math.trunc(값)정수 부분만 반환
임의 수 다루기
Math.random()부동 소수점의 유사 난수 반환(0<=x<1)Math.floor(Math.random()*10)1자리 수 난수 반환 (1<= x<10)
Math.floor(Math.random() * 100); //0이상 100 미만
10 + Math.floor(Math.random() * 10); //10이상 20 미만
안전한 난수 사용
crypto.getRandomValues(타입지정배열)난수 배열 반환- 비밀번호 등 정보보호가 중요한 문자열 생성
//임의의 정수(부호 없는 16비트) 10개를 담은 배열 생성
const randomArr = crypto.getRandomValues(new Uint16Array(10));
//배열 요소를 연결해 난수 생성
randomArr.join('');
Array
push()맨 뒤에 stack 추가pop()맨 뒤에 Stack 출력 및 삭제shift()맨 앞에 Queue 출력 및 삭제unshift()맨 앞에 Queue 추가concat()array 결합includes()특정 값 포함 여부 T/F 확인indexOf()특정 값 인덱스 위치 확인 (불포함 시 -1)reverse()배열 순서 뒤집기, 원본 값에 반영slice(start, end)배열 일부 복사splice(start, end, items...)다중 값 제거, 추가sort()index[n][0] 값을 비교해 오름차순 정렬
forEach
- 배열에 대해 콜백 함수를 이용해 배열 값 각각을 연산, 출력
- for..of문 등장 이전 사용
numbers.forEach(function(num){ //익명의 매개변수 num
console.log(num);
})
for (let num of numbers({
console.log(num);
}
map
- 배열 값을 연산한 콜백 함수 반환 값으로 새로운 배열 생성
const firstNum = numbers.map(function(num){ //익명의 매개변수 num
return num.first;
})
filter
- 조건문의 결과에 따라 배열의 부분집합으로 새 배열 생성
function validUserNames(usernames) {
return usernames.filter(username=>{
return username.length<10;
})
}
some
true/false 반환, 조건에 대해 하나라도 true면 true
function selectiveEvents(numbers){
return numbers.some(number=>number%2===0);
}
every
true/false 반환, 조건에 대해 모든 값이 true면 true
function allEvents(numbers){
return numbers.every(number=>number%2===0);
}
reduce
- 배열을 순환, 반복문을 통한 배열 값 연산, 비교와 유사
- accumulator 반환값 누적, 배열 개별 요소
//합계
array.reduce((accumulator, currentValue)=>{
return accumulator + currentValue;
})
//최대값
array.reduce((accumulator, currentVallue)=>{
if (currentValue>accumulator){
return currentValue;
}
return accumulator;
})
Object
- 객체 내에서 함수를 정의하고, 사용
- a function stored as a property
- 접근: objectName.methodName(); ```js const obj = { mtd1 : function(args){ return ; }, mtd2 : function(args){ return ; } }
obj.mtd1(arg); ```
Execute related..
.setTimeout(func(), milisec)
- 일정 시간 후 함수 실행
.setInterval(func(), milisec)
- 일정 시간이 지날때 마다 함수 반복 실행
.clearInterval(id)
- 실행 중인 interval 중단