Posts

Javascript var, let & const

Image
  For a long time, we all know var keyword in javascript to declare a variable. However, ES6 (ECMAScript 2015) introduces 2 new keywords to declare variables, namely let & const . There are several differences between var , let & const , which we can classify into several types, namely related to scope , hoisting , the ability of these variables to be reassigned & redeclare . Below I will try to explain them one by one. Scope Scope is the current context of code, which determines the variable’s accessibility to JavaScript. In short, I often refer to scope as a place for variables to live, where they can be called/accessed. The keyword var has a function scope , that is, it only recognizes function blocks (other than global scope) as its scope, while let & const has block scope , that is, every block marked with curly brackets { … } is also considered as a context/scope. Hoisting Hoisting is a process in which javascript appears to move a function , variable , ...

Javascript var, let & const (bahasa indonesia)

Image
  Kita lebih dahulu mengenal kata kunci var dalam javascript untuk mendeklarasikan sebuah variabel. Namun ES6 (ECMAScript 2015) memperkenalkan 2 buah kata kunci baru untuk mendeklarasikan variabel, yakni let & const . Ada beberapa perbedaan antara var, let & const, yang dapat kita golongkan menjadi beberapa jenis, yakni terkait scope , hoisting , kemampuan variabel tersebut untuk di- reassign & di- redeclare . Di bawah ini saya akan coba menjelaskannya satu persatu. Scope Scope adalah konteks kode saat ini, yang menentukan aksesibilitas variabel ke JavaScript. Singkatnya, saya sering menyebut scope sebagai tempat hidup variabel, dimana variabel tersebut dapat dipanggil/diakses. Kata kunci var memiliki scope yang sifatnya functional scope , yakni hanya mengenal block fungsi (selain global scope) sebagai scope-nya, sedangkan let & const memiliki scope yang sifatnya block scope , yakni setiap block yang ditandai dengan tanda kurung kurawal { … } juga dianggap s...