본문 바로가기

자바스크립트

(33)
JavaScript 표준 내장 객체 - Array(2) findIndex const numbers = [17,20,199,5,48] const foundIndex = numbers.findIndex(item=>itemitem>100)) // 2 const users = [ {name:'A', age: 12, email:'123@456.789'}, {name:'B', age: 15, email:'987@654.321'}, {name:'C', age: 18}, ] console.log(users.findIndex(user=> !user.email))// 2 console.log(users.findIndex(user=> user.call))// -1 - 배열 내 아이템에 콜백 테스트를 실행하여 처음으로 통과하는 요소의 인덱스를 반환합니다. - 테스트 통과 후 이후..
JavaScript 표준 내장 객체 - Array(1) length const fruits = ['Apple', 'Banana', 'Cherry'] console.log(fruits.length) // 3 - 배열 데이터의 길이를 확인할 수 있습니다. at console.log(fruits.at(1)) // banana console.log(fruits.at(-1)) // cherry - 배열의 인덱스를 확인할 수 있습니다. - 0부터 length-1까지 인덱싱할 수 있으며, 음수 입력시 역순으로 인덱싱합니다. 이 경우에는 -1부터 -length 값을 입력하여 사용합니다. concat const item1 = ['Apple','Banana','Cherry'] const item2 = ['Cherry', 'Durian'] const item3 = item1.c..
JavaScript 표준 내장 객체 - Date Date 클래스 호출 let date = new Date() console.log(date) let date2 = new Date(2024,11,16,17,23,11) - Date(날짜) 표준 내장 객체를 사용하기위해 Date 클래스를 먼저 선언합니다. - 기본값을 설정한 클래스를 출력 시 현재 시간을 출력합니다. - Date 클래스에 인수를 입력하여 특정 타임스탬프를 담아놓을 수 있습니다. Date 내 메서드 console.log(date.getFullYear())// 년 console.log(date.getMonth())// 월 (0~11) console.log(date.getDate())// 일 console.log(date.getDay())// 요일 (0~6) console.log(date.get..
JavaScript 표준 내장 객체 - Math abs (absolute) console.log(Math.abs(7)) console.log(Math.abs(-7)) console.log(Math.abs(3.14)) console.log(Math.abs(-3.14)) console.log(Math.abs('7.35')) console.log(Math.abs('-7.35')) - 숫자 데이터의 절댓값을 반환하는 메서드입니다. - 문자를 입력하여도 Number로 형변환하여 절댓값을 적용해줍니다. ceil & floor & round console.log(Math.ceil(3.141592))// 4 console.log(Math.floor(3.141592))// 3 console.log(Math.round(3.141592))// 3 console.log(M..
JavaScript 표준 내장 객체 - Number toFixed const pi = 3.14159265358979 console.log(pi.toFixed(2)) // '3.14' // 숫자로 사용하기 위해서는 Number로 형변환을 해주어야 합니다. console.log(Number(num.toFixed(2)) // 3.14 - 지정된 자리수까지 표현하는 새로운 문자를 반환합니다. - 숫자 데이터로 사용하기 위해서는 Number() 를 사용하여 숫자 데이터로 형변환을 시켜야 합니다. toLocaleString const num1 = 1000 const num2 = 100000000 const num3 = num1.toLocaleString() const num4 = num2.toLocaleString() console.log(num1) // 1000 ..
JavaScript 표준 내장 객체 - String String 데이터에 사용할 수 있는 표준 내장 객체중, 자주 쓰는 것들을 정리한 글입니다. length const msg1 = 'Hello World' console.log(msg1.length) // 11 // 응용 예시 const msg2 = 'The quick brown fox jumps over the lazy dog' const h1El = document.querySelector('h1') h1El.textContent = msg2.length>20? `${msg.slice(0,20)}...`:msg2 - 문자의 길이를 숫자로 반환합니다. includes const msg1 = 'Hello World!' const msg2 = 'The quick brown fox jumps over the ..
JavaScript 함수 - 호출 스케줄링 setTimeout const timeout = setTimeout(()=>{ console.log('Hello') }, 5000) - 지정된 콜백 함수를 시간차를 두고 호출하는 함수입니다. - setTimeout( [콜백함수] , 시간 ) 의 형태로 구성되며, 시간의 단위는 ms (1000분의 1초) 입니다. clearTimeout const timeout = setTimeout(()=>{ console.log('Hello') }, 5000) const btnEl = document.querySelector('button') btn.addEventListener('click', ()=>{ console.log('타임아웃 취소') clearTimeout(timeout) }) - 설정된 setTimeout..
JavaScript 함수 - 화살표함수 와 콜백함수, 즉시실행함수 화살표 함수 (Arrow Function) function hello() { return 'Hello' } // 반환값만 존재하는경우 중괄호를 생략할 수 있습니다. const hello_arrow = () => 'Hello' // 매개변수가 한개만 있는 경우 소괄호를 생략할 수 있습니다. const say_my_name = name => 'my name is '+name console.log(hello()) console.log(hello_arrow) // 매개변수가 2개 이상이면 소괄호를 생략할 수 없습니다. const add = (a,b) => a+b // 함수 내 다른 구문이 존재한다면 중괄호를 생략할 수 없습니다. const console_arrow = c =>{ console.log(c) }​ ..