본문 바로가기

Python_WEB/JavaScript

[freecodecamp]JavaScript 지역 변수 및 기능

반응형

함수 내에서 선언 된 변수와 함수 매개 변수에는 로컬 범위가 있습니다.

즉, 해당 기능 내에서만 볼 수 있습니다.

다음은 loc이라는 지역 변수가있는 myTest 함수입니다.

 

function myTest() {
  var loc = "foo";
  console.log(loc);
}
myTest(); // logs "foo"
console.log(loc); // loc is not defined

 

loc은 함수 외부에서 정의되지 않았습니다.

편집기에는 무슨 일이 일어나고 있는지 확인하는 데 도움이되는 두 개의 console.log가 있습니다.

코드를 작성하면서 콘솔이 어떻게 변경되는지 확인하십시오.

myLocalScope 내에서 로컬 변수 myVar를 선언하고 테스트를 실행합니다.

콘솔에는 여전히 'ReferenceError : myVar is not defined'가 있지만 이로 인해 테스트가 실패하지는 않습니다.

 

Q>

1. 코드는 전역 myVar 변수를 포함하지 않아야합니다.

2. 로컬 myVar 변수를 추가해야합니다.

 

A>

function myLocalScope() {
    'use strict';
  
    // Only change code below this line
    var myVar;
    console.log('inside myLocalScope', myVar);
  }
  myLocalScope();
  
  // Run and check the console
  // myVar is not defined outside of myLocalScope
  console.log('outside myLocalScope', myVar);
 

 

https://www.freecodecamp.org/

 

freeCodeCamp.org

Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.

www.freecodecamp.org

 

반응형