DEV Community

Cover image for VAR, LET, DAN CONST JAVASCRIPT ES6
Muhammad Mahib Arib
Muhammad Mahib Arib

Posted on

4 4

VAR, LET, DAN CONST JAVASCRIPT ES6

Pada artikel kali ini saya akan menjelaskan perbedaan dalam penggunaan var, let, dan const pada JavaScript ES6.

CONST

Nilainya hanya dapat dibuat sekali dan hanya bisa dipakai di dalam scope.

if(true){
 const nama = "John Doe";
 nama = "Steven"; // error, karena tidak boleh ganti nilai
}

console.log(nama); // error, karena di luar if
Enter fullscreen mode Exit fullscreen mode

LET

Nilainya bisa diubah dan hanya bisa dipakai di dalam scope.

if(true){
 let nama = "John Doe";
 nama = "Steven"; // berhasil ganti nilai
}

console.log(nama); // error, karena di luar if
Enter fullscreen mode Exit fullscreen mode

VAR

Nilainya bisa diubah dan bisa dipakai di luar scope.

if(true){
 var nama = "John Doe";
 nama = "Steven"; // berhasil ganti nilai
}

console.log(nama); // berhasil walaupun di luar if
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more