Variabel yang dideklarasikan di bagian atas program atau di luar fungsi dianggap sebagai variabel lingkup global
Mari kita lihat contoh variabel lingkup global
// program to print a text let a = "hello"; function greet () { console.log(a); } greet(); // helloPada program di atas, variabel a dideklarasikan di bagian atas program dan merupakan variabel global. Ini berarti variabel a dapat digunakan di mana saja dalam program
Nilai variabel global dapat diubah di dalam suatu fungsi. Sebagai contoh,
// program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 3_Pada program di atas, variabel a merupakan variabel global. Nilai a adalah halo. Kemudian variabel a diakses di dalam fungsi dan nilainya berubah menjadi 3
Karenanya, nilai suatu perubahan setelah mengubahnya di dalam fungsi
Catatan. Sebaiknya hindari penggunaan variabel global karena nilai variabel global dapat berubah di berbagai area dalam program. Itu dapat memperkenalkan hasil yang tidak diketahui dalam program
Dalam JavaScript, variabel juga dapat digunakan tanpa mendeklarasikannya. Jika suatu variabel digunakan tanpa mendeklarasikannya, variabel itu secara otomatis menjadi variabel global
Sebagai contoh,
function greet() { a = "hello" } greet(); console.log(a); // helloPada program di atas, variabel a merupakan variabel global
Jika variabel dideklarasikan menggunakan let a = "hello", program akan menampilkan kesalahan
Catatan. Dalam JavaScript, ada "strict mode";_ di mana variabel tidak dapat digunakan tanpa mendeklarasikannya. Untuk mempelajari lebih lanjut tentang ketat, kunjungi JavaScript Ketat
Lingkup Lokal
Variabel juga dapat memiliki cakupan lokal, mis. e itu hanya dapat diakses dalam suatu fungsi
Contoh 1. Variabel Cakupan Lokal
// program showing local scope of a variable let a = "hello"; function greet() { let b = "World" console.log(a + b); } greet(); console.log(a + b); // errorKeluaran
helloWorld Uncaught ReferenceError: b is not defined_Pada program di atas, variabel a merupakan variabel global dan variabel b merupakan variabel lokal. Variabel b hanya dapat diakses di dalam fungsi salam. Oleh karena itu, ketika kami mencoba mengakses variabel b di luar fungsi, terjadi kesalahan
biarkan adalah Blok Lingkup
Kata kunci // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 30 adalah cakupan blok (variabel hanya dapat diakses di blok langsung)
Contoh 2. Variabel cakupan blok
// program showing block-scoped concept // global variable let a = 'Hello'; function greet() { // local variable let b = 'World'; console.log(a + ' ' + b); if (b == 'World') { // block-scoped variable let c = 'hello'; console.log(a + ' ' + b + ' ' + c); } // variable c cannot be accessed here console.log(a + ' ' + b + ' ' + c); } greet();_Keluaran
Hello World Hello World hello Uncaught ReferenceError: c is not definedPada program di atas, variabel
- a adalah variabel global. Itu dapat diakses di mana saja dalam program ini
- b adalah variabel lokal. Ini hanya dapat diakses di dalam fungsi // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 31
- c adalah variabel cakupan blok. Itu hanya dapat diakses di dalam blok pernyataan // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 32
Oleh karena itu, dalam program di atas, dua yang pertama // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 33 bekerja tanpa masalah apapun
Namun, kami mencoba mengakses variabel cakupan blok c di luar blok di // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 33 ketiga. Ini akan menimbulkan kesalahan
Catatan. Dalam JavaScript, // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 3_5 adalah cakupan fungsi dan // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 30 adalah cakupan blok. Jika Anda mencoba menggunakan // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 3_7 di dalam pernyataan // program to show the change in global variable let a = "hello"; function greet() { a = 3; } // before the function call console.log(a); //after the function call greet(); console.log(a); // 32 dalam program di atas, seluruh program berfungsi, karena c diperlakukan sebagai variabel lokal