forked from bellshade/Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionScope.js
More file actions
38 lines (30 loc) · 968 Bytes
/
functionScope.js
File metadata and controls
38 lines (30 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// == Contoh == //
// 1. Mendefisikan variabel didalam fungsi
function memancing() {
let ikan = "Lele";
console.log(ikan);
}
// Menampilkan variabel ikan yang didefinisikan didalam fungsi memancing()
// eslint-disable-next-line no-undef, no-use-before-define
console.log(ikan); // Error: ikan tidak terdefinisikan
memancing(); // Lele
// 2. Mendefisikan variabel diluar fungsi
let ikan = "Gurame";
function kolamIkan() {
console.log(ikan);
}
// Menampilkan variabel ikan yang didefinisikan diluar fungsi kolamIkan()
console.log(ikan); // Gurame
kolamIkan(); // Gurame
// 3. Deklarasi variabel didalam dan diluar fungsi
let noktunal = "Kelelawar";
let diurnal = "Gajah";
function hewan() {
let nokturnal = "Burung Hantu";
console.log(nokturnal);
console.log(diurnal);
}
// Menampilkan variabel mamalia yang didefinisikan didalam dan diluar fungsi
console.log(diurnal); // Gajah
console.log(noktunal); // Kelelawar
hewan(); // Burung Hantu & // Gajah