본문 바로가기

자바스크립트19

[JAVASCRIPT] 날짜 확인하기 Date 객체 사용 방법 Date 자바스크립트 Date 내장 객체를 활용해서 날짜, 시간과 관련된 연산들을 쉽게 할 수 있습니다. Date 객체 생성 생성자로 호출하여 Date 객체를 반환받을 수 있습니다. 생성자를 호출하는 시점의 시간으로 Date 객체가 생성됩니다. const date = new Date(); console.log(date); // "Tue Aug 06 2019 21:16:22 GMT+0900 (한국 표준시)" Date 객체에 있는 날짜, 시간 반환 메소드 메소드 반환값 범위 getFullYear() 연도 4자리 getMonth() 월 0-11 getDate() 일 1-31 getDay() 요일 0-6 (0:일요일, 6:토요일) getHours() 시 0-23 getMinutes() 분 0-59 getSeco.. 2019. 8. 6.
[JAVASCRIPT] 문자열 혹은 배열이 특정 요소를 포함하는지 확인 - includes() includes 1) 문자열 안에서 특정 단어를 찾을 때, 2) 배열의 요소들 중에서 특정 요소가 포함되어 있는지 확인할 때 모두 includes 메소드를 사용할 수 있습니다. 단 전자는 String prototype에 있는 includes 메소드이고, 후자는 Array prototype에 있는 includes 메소드입니다 (최하단의 MDN 문서를 확인해 주세요) 1. 문자열에서 찾기 str.includes(searchString); str.includes(searchString, position); searchString 찾을 문자열 position 시작 인덱스, default 0 예시 문자열에 searchString이 포함되어 있으면 true, 없으면 false. const message = "Eve.. 2019. 8. 6.
[JAVASCRIPT] 문자열 합치기 - concat() / 배열의 요소들을 하나의 문자열로 합치기 - join() concat / join 자바스크립트에서 문자열을 합치는 방법입니다. 1. + 사용 const str1 = "Hello"; const str2 = " World"; console.log(str1 + str2); // Hello World 2. 여러 개의 문자열 합치기 concat str.concat(string2, string3[, ..., stringN]); str에 인자로 전달한 문자열들을 모두 합쳐서 새로운 문자열을 반환합니다. 예시 1 두 문자열을 합칩니다. const strA = "A friend to all"; const strB = "is a friend to none"; const message = strA.concat(strB); console.log(message); // "A frie.. 2019. 8. 6.
[JAVASCRIPT] 문자열 나누기 - split() / 부분 문자열 추출하기 - substring() split / substring 자바스크립트에서 문자열 데이터를 나누거나 추출하는 방법입니다. 1. 문자열 나누기 - split() str.split(separator); 구분자(separator)를 기준으로 문자열을 나누고 나눠진 영역들을 요소로 하는 배열을 반환합니다. 예시 1 const message1 = "Still waters run deep."; const arr1 = message1.split(" "); console.log(arr1); // ["Still", "waters", "run", "deep."] 구분자로 공백(" ")을 전달하여 공백을 기준으로 문자열 나눴습니다. 예시 2 const message2 = "name: John/age: 30/country: korea"; const a.. 2019. 8. 6.
[JAVASCRIPT] 문자열(string)의 첫 글자를 대문자로 바꾸기 영문 첫 글자를 대문자로 영어에는 첫 글자를 대문자로 표기하는 대문자 표기규칙(Capitalization)이 있는데요. 자바스크립트를 사용해서 Capitalization을 쉽게 구현할 수 있습니다. 다음의 두 가지 문자열 메소드를 활용합니다. charAt str.charAt(index) 문자열에서 인자로 전달한 index에 해당하는 단일문자를 반환합니다. slice str.slice(beginIndex[, endIndex]) 문자열의 beginIndex부터 endIndex 전까지의 부분문자열을 추출합니다. endIndex가 주어지지 않으면 beginIndex부터 마지막 문자까지 포함하는 문자열을 추출합니다. 예시 "i like coding"이라는 문자열이 있습니다. const str = "i like c.. 2019. 8. 5.
[JAVASCRIPT] 배열(Array)에서 특정값 삭제하기 - splice() splice 자바스크립트의 splice() 메서드를 활용해서 배열의 특정값을 삭제하는 방법입니다. 구문 array.splice(start, deleteCount, item1, item2, ...) start 변경을 시작할 배열의 인덱스. deleteCount 배열에서 제거할 요소의 개수 item (optional)배열에 추가할 요소 const fruits = ['Apple', 'Banana', 'Orange', 'Mango', 'Grape']; const index = fruits.indexOf('Orange'); if (index !== -1) { fruits.splice(index, 1); } console.log(fruits); // 결과: ['Apple', 'Banana', 'Mango', 'Gr.. 2019. 8. 4.
[JAVASCRIPT] switch 사용법 break, default (if else문과 비교) switch switch는 조건에 따라 다양한 행동을 수행하는 데 사용됩니다. 여러 개의 if-else 문을 사용하는 대신에 사용할 수 있으며, 경우에 따라 if-else보다 코드를 더 읽기 쉽고 관리하기 좋게 만들어 줍니다. 기본사용법 switch(expression) { case value1: // 표현식이 value1과 일치할 때 실행할 코드 break; case value2: // 표현식이 value2와 일치할 때 실행할 코드 break; // 원하는 만큼의 case를 가질 수 있음 default: // 어떤 case도 일치하지 않을 때 실행할 코드 } 예시 const fruit = 'apple'; switch(fruit) { case 'apple': console.log('Apple is sel.. 2019. 7. 31.


        
답변을 생성하고 있어요.