// LET and CONST ********************************************
// :변수와 상수
// ES5
var name5='Jane Smith';
var age5 = 23;
name5 = 'Jane Miller';
console.log(name5); //output// Jane Miller
// ES5에서는 변수와 상수의 구분이 x
// ES6
const name6 = 'Jane Smith';
let age6 = 23;
name6 ='Jane Miller';
console.log(name6); //output// VM84:3 Uncaught TypeError: Assignment to constant variable.
// const: constant variable: 상수
// let: in ES6 we don't use var anymore. let is replaced var.
/*
var와 let의 차이점
- var: function scope -> 선언되어진 함수 function 안에서라면 변수가 선언되어진 해당블록 밖에서도 접근가능
- let: blocked scope -> 변수는 변수가 선언된 블록 {}안에서만 접근 가능 (자바랑 비슷)
*/
// ES5
function driverLicence5(passedTest){
if (passedTest){
var firstName = 'John';
var yearOfBirth = 1990;
}
console.log(firstName + ', born in ' + yearOfBirth + '. is now officially allowed to drive a car.');
}
driverLicence5(true);
// output // John, born in 1990. is now officially allowed to drive a car.
// ES6
function driverLicence6(passedTest){
if (passedTest){
let firstName = 'John';
const yearOfBirth = 1990;
}
console.log(firstName + ', born in ' + yearOfBirth + '. is now officially allowed to drive a car.');
}
driverLicence6(true);
// output // VM143:23 Uncaught ReferenceError: firstName is not defined ->접근불가
// ES6 사용가능한 예시
function driverLicence6(passedTest){
let firstName;
const yearOfBirth = 1990; //const상수는 블록안에서 재정의 하는게 불가능(자바랑 마찬가지)
if (passedTest){
firstName = 'John';
}
console.log(firstName + ', born in ' + yearOfBirth + '. is now officially allowed to drive a car.');
}
driverLicence6(true);
// ex of let
let i = 23;
for(let i = 0; i < 5 ; i++){
console.log(i);//이곳의 i와
}
console.log(i);//이곳의 i는 다르다.
//output
/*
0
1
2
3
4
23
*/