Quick actions

cmd+k|ctrl+k

Navigation

Languages

javascript-dasar-variabelScope

Snippet info

Language

JavaScript

Visibility

public

Author

bintangaji

Created

2020-05-22T15:41:36Z

Updated

2020-05-22T15:41:36Z

// global variable, dapat diakses pada parent() dan child()
const a = 'a'; 

function parent() {
    // local variable, dapat diakses pada parent() dan child(), tetapi tidak dapat diakses diluar dari fungsi tersebut.
    const b = 'b'; 
    
    function child() {
        // local varible, dapat diakses hanya pada fungsi child().
        const c = 'c';
    }
}

// Coba variable scope
// =================================

// Tanpa mendeklarasi variable total
function multiply(num) {
    total = num * num;
    return total;
}
 
let total = 9;
let number  = multiply(10);
 
console.log(total)
 
/* output
400
*/

// ==================================
// mendeklarasi variable total
function multiply(num) {
    let total = num * num;
    return total;
}
 
let total = 9;
let number  = multiply(10);
 
console.log(total)
 
// output
// 9
INFO