
Mengenal Svelte JS: Framework JavaScript yang Ringan, Cepat, dan Disukai Developer
Mengenal Svelte JS sebagai framework JavaScript bukanlah hal baru
25 April, 2025

In modern JavaScript development, choosing how to declare variables is one of the things developers need to consider.
Although JavaScript provides several options such as var, let, and const, each has different characteristics and intended uses. Choosing the right variable declaration can make code easier to read, maintain, and develop.
One approach that is widely used in modern JavaScript is using Const as the primary choice when a variable does not need to be changed after it is declared.
This approach provides clear restrictions on variables, allowing developers to know that a particular value or reference will not be reassigned during program execution.
In practice, not every situation can be handled with only const. There are certain situations where the value of a variable needs to change, making let the more appropriate choice.
Meanwhile, var, which was previously widely used, is now increasingly avoided in modern JavaScript development because its scope and hoisting characteristics can potentially cause problems.
Using Const can be the primary choice when a variable only needs to be declared and used without reassignment. In other words, after a variable is created using const, developers cannot assign a new value to that variable.
The following code provides an example:
const nama = "Iqbal";
console.log(nama);
In this example, the variable nama has the value "Iqbal", and that value cannot be replaced with a new assignment. If a developer tries to make a change as follows:
const nama = "Iqbal";
nama = "Niyaz";
JavaScript will generate an error because a variable declared using const cannot receive a new assignment.
This simple concept provides significant benefits in application development. Developers can know that a variable has a fixed binding, reducing the risk of its value being changed accidentally.
This is especially helpful when a project contains many files, functions, components, and developers working together.
In addition, using const makes the intent of the code clearer. When developers see a const declaration, they can understand that the variable is not intended to receive a new value.
The code becomes easier to understand without having to trace the entire program to determine whether the variable's value has ever changed.
Although Using Const is highly recommended whenever possible, that does not mean let is unnecessary. let is used when a variable actually needs to change its value while the program is running.
An example can be found in a calculation process:
let total = 0;
total = total + 100;
total = total + 200;
console.log(total);
In this example, the value of total changes. Therefore, let is a more appropriate choice than const.
Another case can be found when the value of a variable is determined based on a particular process or user input. For example, an application may need to determine a status based on the result of a particular process. The value may not be known when the variable is created and may change later.
let status = "pending";
if (pembayaranBerhasil) {
status = "success";
}
Using let in a situation like this is more appropriate because the status variable is designed to receive a new value. Forcing the use of const in such a situation can actually make the code less practical.
However, let should also not be used excessively. If a variable never actually undergoes reassignment, const is usually the more appropriate choice.
By limiting the use of let to variables that actually change, the code structure can become more consistent.

Before ES6 was introduced, var was the primary way to declare variables in JavaScript. However, the evolution of JavaScript introduced let and const with mechanisms that are better suited to modern development needs.
One of the main reasons developers have started avoiding var is its function scope characteristic. Unlike let and const, which have block scope, variables declared with var are not limited to blocks such as if, for, or while.
For example:
if (true) {
var nama = "Iqbal";
}
console.log(nama);
The nama variable can still be accessed outside the if block. Situations like this can increase the possibility of conflicts or unexpected value changes in complex code.
In addition to scope issues, var also has hoisting behavior that can often confuse developers, especially when the code is not well understood. var declarations are processed before code execution, allowing the variable to be recognized before its declaration line is executed, although its value at that point is undefined.
console.log(nama);
var nama = "Iqbal";
The result does not immediately produce a ReferenceError, but rather undefined. This behavior differs from let and const, which have a Temporal Dead Zone (TDZ) before their declarations are executed.
In modern JavaScript development, variable declarations can follow a simple rule. Use const as the first choice when a variable does not require reassignment.
If the variable's value needs to change, use let. Meanwhile, var should be avoided in new code unless there is a specific reason to keep using it.
This approach does not mean that const must be used for every variable. Developers still need to understand the purpose of each variable before determining its declaration. The main principle is to choose the declaration that best describes how the variable will be used.
For example :
const nama = "Iqbal";
const usia = 25;
let status = "Belum Menikah";
status = "Menikah";
In this code, nama and usia do not need to change, so they use const. Meanwhile, status can change, so it uses let.
Using this pattern can also help with the debugging process. When developers encounter a const variable, they can immediately eliminate the possibility that the variable has undergone reassignment. Conversely, when they encounter let, they know that the variable's value may change.
This becomes increasingly important in large-scale projects. The more code is created, the greater the need to maintain consistency and readability. Simple rules for choosing between const and let can become part of a team's coding standards.
In simple terms, const can be the default choice, let should be used when a value needs to change, while var should generally not be used for new JavaScript code.
const provides a clear restriction because a variable cannot receive a new assignment after declaration. let offers flexibility when a value needs to be updated. Meanwhile, var has legacy characteristics that can make scope management more difficult.
However, it is important to understand that const does not mean that the entire contents of an object or array become immutable. For example:
const mahasiswa = {
nama: "Iqbal",
kelas: 304
};
mahasiswa.kelas = 305;
console.log(mahasiswa);
The code can still modify the object's properties. What is not allowed is replacing the mahasiswa reference with a new object using assignment.
The same concept applies to arrays:
const mahasiswa = ["Iqbal", "Niyaz"];
mahasiswa.push("Dwi");
console.log(mahasiswa);
The array can still be modified because const protects the binding or reference, rather than making the contents of an object or array completely immutable.
Understanding this concept is important so developers do not misunderstand the purpose of const. By understanding the difference between reassignment and mutation, const can be applied more appropriately across various types of JavaScript projects.
Using Const is an excellent approach for building JavaScript code that is more structured, reliable, and easier to understand. const should be the first choice when a variable does not require a value change or reassignment after it is declared.
On the other hand, let still plays an important role when a variable actually needs to change during program execution. Using let is not something that should be avoided, but it should only be applied when there is a need for reassignment.
Meanwhile, var should generally be avoided in modern JavaScript development because its function scope, hoisting, and redeclaration behavior can increase the potential for errors in a project.
By understanding the differences between the three, developers can make better decisions when declaring variables. The simple principle is use const as the default, use let when the value needs to change, and avoid var in new code. This approach can help produce JavaScript that is cleaner, more consistent, easier to maintain, and aligned with modern development practices.
Photo Source : Growtika on Unsplash