javas script : Global Scope

Global Scope

Kami akan mulai dengan lingkup global. Variabel yang didefinisikan dalam lingkup global dinyatakan di luar satu set kurung kurawal {}, disebut sebagai block, dan dengan demikian tersedia di seluruh program. Kami akan membahas lebih lanjut tentang blok di latihan berikutnya.
Mari kita lihat contoh global scope.
const color = 'blue' const colorOfSky = () => { return color; // blue }; console.log(colorOfSky()); // blue
  1. Here the variable color is declared outside of the function block, giving it global scope.
  2. In turn, color can be accessed within the colorOfSky function.
Global variables make data accessible from any place within a program.
1.
At the top of sky.js, write two global variables using const, one named satellite set equal to 'The Moon', the other named galaxy set equal to 'The Milky Way'.
2.
Using let, write another global variable named stars and set it equal to 'North Star'.
3.
Below these variables, using const, write a function named myNightSky. Inside the function, include a return statement like this:
return 'Night Sky: ' + satellite + ', ' + stars + ', ' + galaxy;
4.
Beneath the myNightSky() function, use console.log() to log the value of myNightSky() to the console.
You'll notice that the myNightSky()function is able to access the global variables without any problem since the variables are available globally.
const satellite = 'The Moon';
const galaxy = 'The Milky Way';

let stars = 'North Star';

const myNightSky = () => {
  stars = 'Sirius';
  return 'Night Sky: ' + satellite + ', ' + stars + ', ' + galaxy;
};

console.log(myNightSky());

Tidak ada komentar:

Posting Komentar