반응형
자바스크립트에는 개발편의를 위해 수많은 객체가 미리 만들어져 있다. 기본적으로 내장된 객체를 표준 내장 객체라고 한다. 문자열을 다루는 String 객체, 배열 자료형을 다루는 Array 객체가 있다. 날짜와 시간을 다루는 Date객체와 수학 수식을 다루는 Math 객체가 있다. 하나씩 연습해보자.
1. String 객체
const pw = "124";
if(pw.length < 4) {
console.log("비밀번호는 최소 4자리 이상 입력해주세요.")
}
const email = "test!naver.com";
if(email.includes("@") === false){
console.log("올바른 이메일 형식이 아닙니다.");
}
2. Array 객체
const arr = [10, 20, 30, 40]
arr.push(50); // 배열 맨 뒤에 50 추가
console.log(arr); // [10, 20, 30, 40, 50]
arr.pop(); // 배열 맨 뒤에서 요소 추출
console.log(arr); // [10, 20, 30, 40]
arr.unshift(0); // 배열 맨 앞에 0추가
console.log(arr); // [0, 10, 20, 30, 40]
arr.shift(); // 배열 맨 앞에서 요소 추출
console.log(arr); // [10, 20, 30, 40]
3. 날짜와 시간을 다루는 Date 객체
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const date = new Date();
alert(date);
</script>
</body>
</html>
4. Math 객체 (난수발생)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const date = new Date();
alert(date);
const random = Math.random();
console.log(random);
</script>
</body>
</html>
반응형
'Javascript' 카테고리의 다른 글
Javascript BMI 계산하는 문제 (0) | 2023.04.03 |
---|---|
Javascript에서 객체 (0) | 2023.04.02 |
Javascript 객체 속성다루기 (0) | 2023.03.30 |
자바스크립트 배열[]안에 중괄호 {} (0) | 2023.03.29 |
Javascript가 HTML문서를 동적으로 변환 시킬 수 있는 이유 (DOM개념) (1) | 2023.03.28 |