본문 바로가기

Programing/JavaScript

(40)
논리연산자 JavaScript는 진리값에 대한 여러 연산을 지원합니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // 논리 부정 (logical NOT) !true; // false !false; // true // 논리합 (logical OR) true || true; // true true || false; // true false || true; // true false || false; // false // 논리곱 (logical AND) true && true; // true true && false; // false false && true; // false false && false; // false // 삼항 연산자 (ternary operator) tru..
Truthy and Falsy Values in JavaScript 1 2 3 4 5 6 7 function logTruthiness (val) { if (val) { console.log("Truthy!"); } else { console.log("Falsy."); } } cs Truthy 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Outputs: "Truthy!" logTruthiness(true); // Outputs: "Truthy!" logTruthiness({}); // Outputs: "Truthy!" logTruthiness([]); // Outputs: "Truthy!" logTruthiness("some string"); // Outputs: "Truthy!" logTruthiness(3.14); // Outputs..
Double Exclamation Mark(느낌표 두개) !!를 사용하게 되면 정의되지 않은 변수/정의된 변수들을 강제로 논리 값으로 변환해주는 기능을 합니다.(Boolean Type Conversion) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var a = 0; console.log(a); // 0 console.log(!a); // true console.log(!!a); // false var b = null; console.log(b); // null console.log(!b); // true console.log(!!b); // false var c = undefined; console.log(c); // undefined console.log(!c); // true console.log(!!c); // false cs..
for...in 문 // 객체에서 중복된 값을 제외하고 객체 추가하기 for...in 문 문법 자바스크립트에서 for...in 문은 객체의 프로퍼티를 순회하는 데 사용된다. 문법은 다음과 같다. 1 2 3 for (변수 in 객체){ 구문 } cs - for...in 문의 본문은 객체의 각 프로퍼티에 대해 한 번씩 실행된다. - 각 반복에 앞서 객체 프로퍼티 중 하나의 이름이 변수에 문자열 타입으로 할당된다. 객체에 대한 for...in 문 예제 1 2 3 4 5 6 7 8 9 10 11 var obj = ; for (var key in obj){ console.log("name: " + key + "; value: " + obj[key]); } /* name: x; value: 10 name: y; value: 11 name: z; value: 12 */ Colored ..
.concat() // 배열, 문자열 합치기 Array.prototype.concat()concat() 메서드는 인자로 주어진 배열이나 값들을 기존 배열에 합쳐서 새 배열을 반환합니다. console.log(배열1.concat(배열2)); 12345var array1 = ['a', 'b', 'c'];var array2 = ['d', 'e', 'f']; console.log(array1.concat(array2));// expected output: Array ["a", "b", "c", "d", "e", "f"]cs String.prototype.concat()concat() 메서드는 매개변수로 전달된 모든 문자열을 호출 문자열에 붙인 새로운 문자열을 반환합니다.console.log(문자열1.concat('문자사이에 추가될 문자나 공백', 문자열2..
.split() / 문자열을 배열로 분할, .split() 으로 특정문자개수 반환하기 MDN문서 String.prototype.split() 이 split()메서드 String는 문자열을 부분 문자열로 분리하여 지정된 구분 문자열을 사용하여 각 분할을 만들 위치를 결정 하여 개체를 문자열 배열로 분할합니다. 원본문자열은 수정되지 않는다!! var 새변수이름 = 문자열변수.split(나누는기준); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 var str = 'The quick brown fox jumps over the lazy dog.'; var words = str.split(' '); console.log(words[3]); // expected output: "fox" var words = str.split(' '); console.log(wor..
.shift() .pop() / 배열에서 요소제거 Array.prototype.shift() shift() 메서드는 배열에서 첫 번째 요소를 제거하고, 제거된 요소를 반환합니다. 이 메서드는 배열의 길이를 변하게 합니다. 1 2 3 4 5 6 7 8 9 var array1 = [1, 2, 3]; var firstElement = array1.shift(); console.log(array1); // expected output: Array [2, 3] console.log(firstElement); // expected output: 1 cs return 배열에서 제거한 요소. 빈 배열의 경우 undefined 를 반환합니다. Array.prototype.pop() / mutable (배열변함) pop() 메서드는 배열에서 마지막 요소를 제거하고 그 요..
.slice() : 범위복사 새 배열로 반환 / .splice() : 배열삭제추가변경 / Array.prototype.push() : 배열에 요소 추가 Array.prototype.slice()출처slice() 메서드는 어떤 배열의 begin부터 end까지(end 미포함)에 대한 얕은 복사본을 새로운 배열 객체로 반환합니다. 원본 배열은 수정되지 않습니다. 배열.slice(포함시작인덱스,끝인덱스(포함x)); 12345678910111213var animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; console.log(animals.slice(2));// expected output: Array ["camel", "duck", "elephant"] console.log(animals.slice(2, 4));// expected output: Array ["camel", "duck"] console.log(..