본문 바로가기

Javascript

Javascript 연습(표준 내장 객체 사용하기)

반응형

자바스크립트에는 개발편의를 위해 수많은 객체가 미리 만들어져 있다. 기본적으로 내장된 객체를 표준 내장 객체라고 한다. 문자열을 다루는 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>

 

반응형