Files
freeCodeCamp/curriculum/challenges/german/02-javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions.md
2022-08-19 20:53:29 +02:00

2.1 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244bf Lokaler Gültigkeitsbereich und Funktionen 1 https://scrimba.com/c/cd62NhM 18227 local-scope-and-functions

--description--

Variablen, die innerhalb einer Funktion deklariert werden, sowie die Funktionsparameter, haben einen lokalen Gültigkeitsbereich. Das bedeutet, dass sie nur innerhalb dieser Funktion sichtbar sind.

Hier ist eine Funktion myTest mit einer lokalen Variable namens loc.

function myTest() {
  const loc = "foo";
  console.log(loc);
}

myTest();
console.log(loc);

Der myTest() Funktionsaufruf wird den String foo in der Konsole anzeigen. Die Zeile console.log(loc) (außerhalb der Funktion myTest) führt zu einem Fehler, da loc außerhalb der Funktion nicht definiert ist.

--instructions--

Der Editor hat zwei console.logs, damit du sehen kannst, was passiert. Überprüfe die Konsole während du programmierst, um zu sehen, wie sie sich verändert. Deklariere eine lokale Variable myVar innerhalb von myLocalScope und führe die Tests aus.

Hinweis: Die Konsole wird immer noch ReferenceError: myVar is not defined anzeigen, aber das wird nicht dazu führen, dass die Tests fehlschlagen.

--hints--

Der Code sollte keine globale Variable myVar enthalten.

function declared() {
  myVar;
}

assert.throws(declared, ReferenceError);

Du solltest eine lokale Variable myVar hinzufügen.

assert(
  /functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
    __helpers.removeWhiteSpace(code)
  )
);

--seed--

--seed-contents--

function myLocalScope() {
  // Only change code below this line

  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

--solutions--

function myLocalScope() {
  // Only change code below this line
  let myVar;
  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);