develog

[ES6] ECMAScript 2015 - ES6 Syntax 본문

카테고리 없음

[ES6] ECMAScript 2015 - ES6 Syntax

냐옴 2021. 5. 7. 09:14

let

  • block scope
  • 재정의 가능
var x = 10;
console.log(x);
{
    let x = 20;
    x = 30;
    console.log(x);
}
console.log(x);

/* console
10
30
10
*/

const

  • block scope
  • 재정의 불가
var x = 10;
console.log(x);
{
    const x = 20;
    x = 30;
    console.log(x);
}
console.log(x);

/* console
10
Uncaught TypeError: Assignment to constant variable.
*/

arrow function

var x = (a, b) => a + b;
console.log(x(1, 2)); // 3

for/of loop

var list = ['red', 'green', 'blue'];
for (x of list) {
    console.log(x);
}

/* console
red
green
blue
*/

//------------------------------------
// for in loop 는 인덱스를 반환
var list = ['red', 'green', 'blue'];
for (x in list) {
    console.log(x, list[x]);
}

/* console
0 red
1 green
2 blue
*/

class

class Member {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}

var member1 = new Member('john', 20);
var member2 = new Member('paul', 21);
console.log(member1.name, member1.age); // john 20
console.log(member2.name, member2.age); // paul 21

Promise

var myPromise = new Promise(function(myResolve, myReject) {
    setTimeout(function() {
        myResolve('hello');
    }, 3000);
});

myPromise.then(function(value) {
    console.log(value);
});

/* console
hello
*/

Symbol

const Person = {
    firstName: 'John',
    lastName: 'Doe',
    age: 20
};

let id = Symbol('id');
Person[id] = 111;
console.log(Person[id]); // 111
console.log(Person.id);  // undefined

default parameter value

function sum(a, b = 2) {
    return a + b;
}
var x = sum(1);
console.log(x); // 3

Function rest parameter

function sum(...args) {
    let sum = 0;
    for (let x of args) {
        sum += x;
    }
    return sum;
}
var x = sum(1, 2, 3);
console.log(x); // 6

Array.find()

  • 조건을 만족하는 첫번쨰 값을 리턴
var list = [3, 5, 2, 1, 4];
var result = list.find(function(value, index, array) {
    return value > 3;
});
console.log(result); // 5

Array.findIndex()

  • 조건을 만족하는 첫번째 값의 인덱스를 리턴
var list = [33, 55, 22, 11, 44];
var result = list.findIndex(function(value, index, array) {
    return value > 33;
});
console.log(result); // 1

New Math Methods

Math.trunc()
Math.sign()
Math.cbrt()
Math.log2()
Math.log10()

New Number Properties

Number.EPSILON
Number.MIN_SAFE_INTEGER
Number.MAX_SAFE_INTEGER
console.log(Number.EPSILON);          // 2.220446049250313e-16
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991

New Number Methods

Number.isInteger()
Number.isSafeInteger()
console.log(Number.isInteger(10));   // true
console.log(Number.isInteger(10.5)); // false

console.log(Number.isSafeInteger(10));                   // true
console.log(Number.isSafeInteger(12345678901234567890)); // false

New Global Methods

isFinite
isNaN
console.log(isFinite(10/0));  // false
console.log(isFinite(10/1));  // true
console.log(isFinite('abc')); // false
console.log(isFinite('123')); // true
console.log(isFinite(123));   // true

console.log(isNaN('abc')); // true
console.log(isNaN('123')); // false
console.log(isNaN(123));   // false

www.w3schools.com/js/js_es6.asp

 

JavaScript ES6

ECMAScript 2015 - ES6 ECMAScript 6 was the second major revision to JavaScript. ECMAScript 6 is also known as ES6 and ECMAScript 2015. This chapter describes the most important features of ES6. New Features in ES6 Browser Support for ES6 (ECMAScript 2015)

www.w3schools.com

 

Comments