function add (x, y){
// 매개변수는 함수 몸체 내부에서만 참조할 수 있다.
// 즉, 매개변수의 스코프(유효범위)는 함수 몸체 내부다.
console.log(x, y);
return x + y;
}
add(2, 5)
// 매개변수는 함수 몸체 내부에서만 참조할 수 있다.
consloe.log(x, y); // ReferenceError: x is not defined
**전역 스코프 (Global scope)**
코드 어디에서든지 참조할 수 있다.
**지역 스코프 (Local scope or Function-level scope)**
함수 코드 블록이 만든 스코프로 함수 자신과 하위 함수에서만 참조할 수 있다.
모든 변수는 스코프를 갖는다. 변수의 관점에서 스코프를 구분하면 다음과 같이 2가지로 나눌 수 있다.
**전역 변수 (Global variable)**전역에서 선언된 변수이며 어디에든 참조할 수 있다.
**지역 변수 (Local variable)**지역(함수) 내에서 선언된 변수이며 그 지역과 그 지역의 하부 지역에서만 참조할 수 있다.
var x = "global x"
var y = "global y"
function outer() {
var z = "outer's local z";
console.log(x); // global x
console.log(y); // global y
console.log(z); // outer's local z
function inner(){
var x = "inner's local x"
console.log(x); // inner's local x
console.log(y); // global y
console.log(z); // outer's local z
}
inner();
}
outer();
console.log(x); // global x
console.log(z); // ReferenceError: z is not defined
function foo(){
console.log('global function foo');
}
function bar(){
function foo(){
console.log('local function foo');
}
foo();
}
bar();
local function foo