표준 내장 객체(Built-in Objects)
- Number 객체
- Math 객체
- Date 객체
- String 객체
- Array 객체
Array 객체
- Array 객체 생성 방법
- new Array( )
- new Array(길이)
- new Array(값1, 값2, ...)
var array = new Array();
document.write(array + '<br>');
document.write(array.length + '<br>');
var array2 = new Array(5);
document.write(array2 + '<br>');
document.write(array2.length + '<br>');
var array3 = new Array(50,70,30,90,200);
document.write(array3 + '<br>');
document.write(array3.length + '<br>');
String 객체
- length 프로퍼티 : 문자열의 길이
var characters = prompt('아이디 입력','5글자 이상');
if(characters.length < 5){
alert('5글자 이상 입력하세요!')
}else{
alert('아이디가 입력되었습니다.');
}
- toUpperCase( ) : 대문자로 변환
var str = 'abcdefg';
alert(str.toUpperCase()); // ABCDEFG
- charAt(인덱스) : 지정한 인덱스의 문자 반환
var msg = '무궁화 꽃이 피었습니다.';
document.write(msg.charAt(0)); // 무
document.write(msg.charAt(4)); // 꽃
- indexOf(문자) : 지정한 문자의 해당하는 인덱스를 앞에서부터 찾아서 반환
- lastIndexOf(문자) : 지정한 문자에 해당하는 인덱스를 뒤에서부터 찾아서 반환
var msg = '대한 사람 대한으로 길이 보전하세';
document.write(msg.indexOf('대') + '<br>'); // 0
document.write(msg.lastIndexOf('대') + '<br>'); // 6
- substring(인덱스) / substr(인덱스) : 지정한 인덱스부터 마지막 인덱스까지의 문자열 추출
- substring(시작인덱스, 끝인덱스) / slice(시작인덱스, 끝인덱스) : 시작 인덱스부터 끝 인덱스 전까지 추출
var msg = '무궁화 꽃이 피었습니다.';
document.write(msg.substring(4) + '<br>'); // 꽃이 피었습니다.
document.write(msg.substr(4) + '<br>'); // 꽃이 피었습니다.
document.write(msg.substring(2,6) + '<br>'); // 화 꽃이
document.write(msg.slice(2,6) + '<br>'); // 화 꽃이
Date 객체
- new Date( ) : 현재 시간으로 Date 객체 생성
var now = new Date();
document.write(now); // Thu Jul 22 2021 14:21:01 GMT+0900 (대한민국 표준시)
- toLocaleString( ) : 국가 포맷을 적용한 날짜와 시간
- toLocaleDateString( ) : 국가 포맷을 적용한 날짜
- toLocaleTimeString( ) : 국가 포맷을 적용한 시간
document.write(now.toLocaleString() + '<br>'); // 2021. 7. 22. 오후 2:21:01
document.write(now.toLocaleDateString() + '<br>'); // 2021. 7. 22.
document.write(now.toLocaleTimeString() + '<br>'); // 오후 2:21:01
- 날짜 요소
document.write('연도 : ' + now.getFullYear() + '<br>');
document.write('월 : ' + (now.getMonth() + 1) + '<br>'); // 0(1월) ~ 11(12월)
document.write('일 : ' + now.getDate() + '<br>');
document.write('요일 : ' + now.getDay() + '<br>') // 0(일) ~ 6(토)
- 시간 요소
document.write('시 : ' + now.getHours() + '<br>'); // 0 ~ 23
document.write('분 : ' + now.getMinutes() + '<br>');
document.write('초 : ' + now.getSeconds() + '<br>')
document.write('밀리초 : ' + now.getMilliseconds()); // 1초 = 1000밀리초
Math 객체
- 소수점 올림, 내림, 반올림
document.write(Math.ceil(5.2) + '<br>'); // 6 : 올림
document.write(Math.floor(5.6) + '<br>'); // 5 : 버림
document.write(Math.round(5.7) + '<br>'); // 6 : 반올림
- max(값1, 값2, ...) : 최대값 구하기
document.write(Math.max(10,20) + '<br>'); // 20
document.write(Math.max(52,70,95,109,43); // 109
- min(값1,값2, ...) : 최소값 구하기
document.write(Math.min(10,20) + '<br>'); // 10
document.write(Math.min(52,70,95,109,43); // 43
- abs(숫자) : 절대값 구하기
document.write(Math.abs(-27)); // 27
- random( ) : 0 ~ 1 사이의 난수 발생
document.write(Math.random());
'Web > JavaScript' 카테고리의 다른 글
[JavaScript] DOM - 태그 생성, 삭제, 스타일 처리 (0) | 2021.08.18 |
---|---|
[JavaScript] DOM(문서 객체 모델) (0) | 2021.08.18 |
[JavaScript] 객체의 종류, BOM(브라우저 객체 모델) (0) | 2021.08.17 |
[JavaScript] 생성자 함수 (0) | 2021.08.17 |
[JavaScript] 객체의 생성, 프로퍼티, 메소드 (0) | 2021.08.13 |