var a = "yeonju";
console.log(a); // expected ouput : yeonju
var a = "changmook";
console.log(a); // expected ouput : changmook
단점 : 코드량이 많아지면 값의 변화에 대해 파악이 힘들다.
→ 그래서 ES6이후 let, const 탄생
let a = "yeonju";
console.log(a); // expected ouput : yeonju
let a = "changmook";
console.log(a);
// Uncaught SyntaxError :
// Identifier 'name' has already been declared
let a = "yeonju";
if (a === "yeonju") {
let a = "changmook";
console.log(a); // expected output: changmook
}
console.log(a); // expected output: yeonju
var x = 'global';
let y = 'global';
console.log(this.x); // "global" 전역 객체의 속성 x를 생성
console.log(this.y); // undefined 전역 객체의 속성 y를 생성하지 않음
const number = "yeonju";
try {
number = "changmook"; // 재 할당 불가능
} catch (err) {
console.log(err);
// expected output: TypeError: invalid assignment to const `number'
}
console.log(number);
// expected output: yeonju
function 함수명() {
// 로직
}
함수명();
호이스팅 가능
코드를 구현한 위치와 관계없이 자바스크립트의 특징인 호이스팅에 따라 브라우저가 자바스크립트를 해석할 떄 맨 위로 끌어 올려진다.
var 함수명 = function() {
// 로직
};
함수명();
-- 실행 전
yeonju(); // 선언식
changmook(); // 표현식
function yeonju() {
return 'small';
}
var changmook = function () {
return 'big';
};
-- 실행 후
function yeonju() { // 위로 올라옴
return 'small';
}
var changmook ; // var만 올라옴. 함수로 인식하지 않고 변수로 인식했다..
yeonju(); // 'small'
changmook (); // Uncaught TypeError: changmook is not a function
changmook = function () {
return 'big;
}
0 == '' //true
0 == '0' //true
1 == true //true
false == '0' //true
null == undefined //true
0 === '' //false
0 === false //false
1 === true //false
NaN === NaN //false
null === undefined //false
XHR객체는 서버와 상호작용하기 위하여 사용. 전체 페이지의 새로고침 없이도 URL로부터 데이터를 받아올 수 있다. (ajax 비동기 통신에 주로 사용)
function reqListener () {
console.log(this.responseText);
}
var request= new XMLHttpRequest();
request.addEventListener("load", reqListener);
request.open("GET", "https://velog.io/@peacebestill");
request.send();
ES6에서는 템플릿 리터럴이라 하는 새로운 문자열 표기법을 도입하였다. '', "" 대신 `(백틱) 문자를 사용한다.
// 이전 -> ', ", + 헷갈려 죽겠다
'<input type="text" value="' +ar[i].title +'" id="'+ ar[i].index+'" disabled/>';
// 백틱 사용
`<input type ="text" value="${ar[i].title}" id="${ar[i].index}" disabled/>`;
// 줄바꿈 \n , 문자열 이어주기 '+' 할 필요 없음
-끗-