Frontend/JS
내장 객체 응용학습
AIHYEONJI
2025. 4. 23. 21:19
* Date
const now = new Date();
console.log("현재:", now.toString());
console.log("년:", now.getFullYear());
console.log("월:", now.getMonth() + 1);
console.log("일:", now.getDate());
const nowTime = now.getTime();
const birthTime = birthday.getTime();
const diffDays = Math.floor((nowTime - birthTime) / (1000 * 60 * 60 * 24));
console.log(`태어난 지 ${diffDays}일 지났습니다.`);
* String
const str1 = "안녕하세요 JavaScript";
const str2 = new String("안녕하세요 JavaScript");
console.log(str1);
console.log(str2);
console.log(str1 == str2);
console.log(str1 === str2);
// length : 문자열의 길이를 저장
console.log(str1.length);
// indexOf() : 특정 문자나 문자열이 처음으로 등장하는 위치를 인덱스로 변환
console.log(str1.indexOf("J"));
console.log(str1.indexOf("Java"));
console.log(str1.indexOf("java")); // -1 = 못찾음
// chatAt() : 특정 문자열에서 전달받은 인덱스에 위치한 문자를 반환
console.log(str1.charAt(7));
// included() : 특정 문자열에서 전달받은 문자열이 포함되어있는지 여부를 반환
console.log(str1.includes("Java"));
console.log(str1.includes("java"));
// substring() : 전달받은 시작 인덱스로부터 종료 인덱스 직전까지의 문자열을 추출
console.log(str1.substring(6)); // 6번부터 끝까지
console.log(str1.substring(6, 10)); // 6부터 9까지
// replace() : 원본 문자열의 일부를 전달받은 문자열로 치환
console.log(str1.replace("안녕하세요", "Hello")); // 안녕하세요 -> Hello로 치환
// split() : 구분자를 기준으로 나눈 후 나뉜 문자열을 하나의 배열에 저장
const str3 = "김사과,오렌지,반하나,이메론,배애리";
const students = str3.split(",");
console.log(students);
for (let i in students) {
console.log(i, students[i]);
}
// trim(): 문자열의 앞 뒤 공백을 제거
const str4 = " JavaScript ";
console.log(`🤍${str4}🤍`);
console.log(`🤍${str4.trim()}🤍`);
// toUpperCase(), toLowerCase() : 문자열을 대,소문자로 변환
console.log(`❤${str.trim().toLowerCase()}❤`)
console.log(`❤${str.trim().toUpperCase()}❤`)
* String --> 시계 만들기
<body>
<script>
function makeClock(){
const date = new Date()
yy = date.getFullYear()
mm = date.getMonth()+1
dd = date.getDay()
hh = date.getHours()
mm = date.getMinutes()
ss = date.getSeconds()
console.log(`${yy}-${mm}-${dd}-${hh}:${mm}:${ss}`)
}
let si
function startClock(){
si = setInterval(makeClock,1000)
}
function stopClock(){
clearInterval(si)
console.log('종료되었음')
}
</script>
<button onclick="startClock()">start</button>
<button onclick="stopClock()">finish</button>
</body>
* Math
// min() : 가장 작은 수를 반환
console.log(Math.min()); // Infinity
console.log(Math.min(1, 10, -10, 1000, 0, "-100"));
// max() : 가장 큰 수를 반환
console.log(Math.max()); //-Infinity
console.log(Math.max(1, 10, -10, 1000, 0, "-100"));
// round() : 소수점 첫번째 자리에서 반올림하여 그 결과를 반환
console.log(Math.round(10.49));
console.log(Math.round(10.59));
console.log(Math.round(-10.5));
console.log(Math.round(-10.51));
// n번째 자리에서 반올림
let num = 123.4567;
console.log(num * 100);
console.log(Math.round(num * 100));
console.log(Math.round(num * 100) / 100);
// console.log(Math.round(num1 * 100) / 100);
console.log(num.toFixed(2));
// floor() : 소수점 첫번쨰 자리에서 소수점을 버림(숫자가 작아져야 함)
console.log(Math.floor(10.49));
console.log(Math.floor(10.5));
console.log(Math.floor(-10.51));
console.log(Math.floor(-10.5));
// cell() : 소수점 첫번째 자리에서 소수점을 올림(숫자가 커져야 함)
console.log(Math.cell(10.49));
console.log(Math.cell(10.5));
console.log(Math.cell(-10.51));
console.log(Math.cell(-10.5));
// random() : 0보다 크거나 같고 1보다 작은 무작위 소수를 반환
console.log(Math.random());
const r = Math.random()
console.log(r)
console.log(Math.cell(r*10)) // 1~10 무작위 수