yngmanie 블로그

es7에 추가된 기능

해당 포스트는 Introducing the new features that ECMAScript 2016 (ES7) adds to JavaScript를 번역하여 작성하였습니다. 잘못된 부분이 있다면 댓글 부탁드립니다.

ECMASript 2015(ES6)가 출시된 이후 이것들은 다양한 새로운 기능들이 소개되었습니다. ES6에는 화살표 함수, sets, maps, classes, destructing 등 많은 것들이 포함되어 있습니다. ES6는 거의 새로운 버전의 자바스크립트를 배우는 것과 같습니다.

Ecam 기술 위원회 39가 ECMA 스펙을 관리합니다. 그들은 매년 새로운 버전의 ECMAScript를 출시하기로 2015년에 결정했습니다. 매년 업데이트되는 ES6와 같이 큰 변화는 없습니다.

ECMAScript 2016은 단지 2개의 새로운 기능만 추가되었습니다.

  • Array.prototype.includes()
  • Exponentiation operator

Array.prototype.includes()

Array.prototype.includes()는 인수로 전달되는 배열의 값을 체크합니다. 값이 있다면 ture를 반환하고 그렇지않으면 false로 반환합니다.

Array.prototype.includes()를 사용하기전에 주어진 배열에 값이 있는지 확인해 봅시다.

let numbers = [1, 2, 3, 4];

if (numbers.indexOf(2) !== -1) {
  console.log("Array contains value");
}

ECMASript2016 에서는 아래와 같이 사용할 수 있습니다.

let numbers = [1, 2, 3, 4];

if (numbers.includes(2)) {
  console.log("Array contains value");
}

Array.prototype.includes()는 NaN을 Array.prototype.indexOf()보다 잘 다룹니다. 만약 배열안에 NaN 포함되어 있다면 indexOf()는 NaN에 대한 정확한 인덱스를 반환하지 않습니다.

Array.prototype.includes()는 NaN에 대한 정확한 인덱스를 반환합니다.

NaN은 자바스크립트 글로벌 객체의 프로퍼티이고 Not-a-Number을 나타냅니다. NaN과 다른 값을 비교할 떼 알려진 흠이 있습니다. 이것들은 Array.prototype.includes()에서 처리되지만 Array.prototype.indexOf()에서는 처리되지 않습니다.

let nubmers = [1, 2, 3, 4, NaN];

console.log(numbers.indexOf(NaN)); //Prints -1;
console.log(numbers.includes(NaN)); //Prints true;

지수계산 연산자

자바스크립트는 이미 +, -, * 등 많은 연산자를 지원하고 있습니다.

ECMAScript 2016에서 지수계산 연산자인 **이 소개되었습니다. 이것은 Math.pow()와 같은 목적입니다. 첫 번째 인수는 두 번쩨 인수의 제곱으로 반환됩니다.

let base = 3;
let exponent = 4;
let result = base ** exponent;

console.log(result); // 81

결론

ECMAScript 2016에 소개된 새로운 기능들은 기존에 존재했던 기능의 대안으로 좀 더 편리하게 사용할 수 있습니다.

ECMAScirpt 2017에는 async/await, SharedArrayBuffer 그리고 Object.prototype에 대한 유용한 메소드들이 포함되어 있습니다.