Var Keyword

Jika suatu variabel didefinisikan dengan keyword var, maka variabel tersebut akan terus ada setelah variabel tersebut dideklarasikan meskipun digunakan di luar blok kode dimana variabel tersebut didefinisikan

In [1]:
var x = 1;
console.log(x);
1
In [2]:
if (true)
{
    var y = 2;
    console.log("y value in if block: ", y);
}

console.log("y value out of if block: ", y);
y value in if block:  2
y value out of if block:  2

Let Keyword

Jika suatu variabel didefinisikan dengan keyword let, maka variabel tersebut hanya berlaku di dalam blok kode dimana variabel tersebut didefinisikan

In [1]:
if (true)
{
    let z = 3;
    console.log("z value in if block: ", z);
    
    if (true)
    {
        console.log("z value in if if block: ", z);
    }
}

console.log("z value out of if block: ", z);
z value in if block:  3
z value in if if block:  3
evalmachine.<anonymous>:12
console.log("z value out of if block: ", z);
                                         ^

ReferenceError: z is not defined
    at evalmachine.<anonymous>:12:42
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at run ([eval]:1002:15)
    at onRunRequest ([eval]:829:18)
    at onMessage ([eval]:789:13)
    at emitTwo (events.js:126:13)
    at process.emit (events.js:214:7)
    at emit (internal/child_process.js:772:12)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)

Variabel yang didefinisikan dengan keyword let dapat diubah nilainya setelah dideklarasikan, sama seperti variabel yang didefinisikan dengan keyword var

In [4]:
let d = 9;
console.log("let before: ", d);

d = 11;
console.log("let after: ", d);
let before:  9
let after:  11

Const Keyword

Sama seperti let, variabel yang didefinisikan dengan keyword const hanya berlaku di dalam blok kode dimana variabel tersebut didefinisikan

In [2]:
if (true)
{
    const p = 3;
    console.log("p value in if block: ", p);
    
    if (true)
    {
        console.log("p value in if if block: ", p);
    }
}

console.log("p value out of if block: ", p);
p value in if block:  3
p value in if if block:  3
evalmachine.<anonymous>:12
console.log("p value out of if block: ", p);
                                         ^

ReferenceError: p is not defined
    at evalmachine.<anonymous>:12:42
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at run ([eval]:1002:15)
    at onRunRequest ([eval]:829:18)
    at onMessage ([eval]:789:13)
    at emitTwo (events.js:126:13)
    at process.emit (events.js:214:7)
    at emit (internal/child_process.js:772:12)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)

Variabel yang didefinisikan dengan keyword const tidak dapat diubah nilainya setelah dideklarasikan. Gunakan keyword ini saat mendefinisikan suatu variabel jika anda yakin bahwa nilai dari variabel tersebut tidak akan berubah. Ini akan berguna untuk membuat kode program menjadi lebih readable

In [5]:
const pi = 3.14; // pi merupakan salah satu konstanta dalam matematika
console.log("const pi before: ", pi);

pi = 0.2929;
console.log("const pi after: ", pi);
evalmachine.<anonymous>:1
const pi = 3.14; // pi merupakan salah satu konstanta dalam matematika
^

SyntaxError: Identifier 'pi' has already been declared
    at evalmachine.<anonymous>:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at run ([eval]:1002:15)
    at onRunRequest ([eval]:829:18)
    at onMessage ([eval]:789:13)
    at emitTwo (events.js:126:13)
    at process.emit (events.js:214:7)
    at emit (internal/child_process.js:772:12)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)

Dengan keyword const, suatu variabel akan bersifat "Immutable", yaitu nilai dari suatu variabel tersebut tidak dapat diubah. Namun diberi tanda kutip karena variabel tersebut tidak immutable seutuhnya.

In [8]:
const obj = {job: "Machine Learning Engineer", description: "Lorem Ipsum"}; // variabel dengan tipe data json
console.log("obj before: ", obj);

obj.description = "Experienced in Python or R";
console.log("obj after: ", obj);
obj before:  { job: 'Machine Learning Engineer', description: 'Lorem Ipsum' }
obj after:  { job: 'Machine Learning Engineer',
  description: 'Experienced in Python or R' }

JSON (akan dipelajari pada materi selanjutnya)

Variabel const dengan tipe data JSON masih dapat diubah propertinya. Agar suatu variabel benar-benar bersifat immutable, anda dapat menggunakan Immutable.js