본문 바로가기

JavaScript

퇴근 후 JS - const

반응형

https://www.w3schools.com/js/js_const.asp

 

JavaScript const

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

1) reassign 불가능하다.

const PI;
PI = 3.14159265359;

 

2) 그런데 , 객체의 property 속성은 변경이 가능하다. 

// You can create a const object:
const car = {type:"Fiat", model:"500", color:"white"};

// You can change a property:
car.color = "red";

// You can add a property:
car.owner = "Johnson";

 

3) 또, 배열의 element 요소 변경이 가능하다.

 

// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];

// You can change an element:
cars[0] = "Toyota";

// You can add an element:
cars.push("Audi");

 

4) 전체 변경은 불가능하다.

const car = {type:"Fiat", model:"500", color:"white"};
car = {type:"Volvo", model:"EX60", color:"red"};    // ERROR

728x90