17/11/2024
const
?In JavaScript, const
is one of the three keywords—let
, const
, and var
—that you can use to declare variables. The primary feature of const
is that it defines a variable that cannot be reassigned. This does not mean that the value assigned to a const
variable cannot change, especially if it’s an object or an array; rather, it ensures that the variable itself cannot be pointed to a different value once it's been initialized.
const
in Action:const PI = 3.14; console.log(PI); // Output: 3.14 // PI = 3.14159; // This will throw an error: TypeError: Assignment to constant variable.
In the example above, trying to reassign PI
will result in an error because const
prevents variable reassignment after its initial declaration.
Although you cannot reassign a const
variable, you can still modify the properties of an object or elements of an array declared with const
. Here's how:
const car = { make: "Toyota", model: "Camry" }; car.model = "Corolla"; // This is allowed console.log(car); // Output: { make: "Toyota", model: "Corolla" } const numbers = [1, 2, 3]; numbers.push(4); // This is allowed console.log(numbers); // Output: [1, 2, 3, 4]
In these examples, while you cannot reassign the variable car
or numbers
, you can change the details within the object or array itself.
Context refers to the value of this
, which is a critical concept in JavaScript. It helps determine how functions and objects behave and influences variable accessibility and scope in JavaScript.
Global Context: This is the default context that exists when you run your JavaScript code. In a browser, the global object is window
, and variables declared with var
, let
, or const
in the global scope can be accessed as properties of window
.
var globalVar = "I am global"; console.log(window.globalVar); // Output: "I am global"
Function Context: When a function is called, a new context is created. Within this function, this
refers to the object that called the function. If the function is called as a method, this
refers to the object that the method is a property of.
const person = { name: "John", greet: function() { console.log("Hello, " + this.name); } }; person.greet(); // Output: "Hello, John"
Block Context: With the introduction of ES6, JavaScript supports block-level scope, which is applicable for variables declared with let
and const
inside braces like functions, loops, and conditionals.
{ const x = 10; console.log(x); // Output: 10 } // console.log(x); // This will throw a ReferenceError
const
and ContextUsing const
improves code readability and maintainability by signaling to other developers (and your future self) that the variable should not be re-assigned. When used correctly, it can prevent bugs that stem from unintended variable reassignments.
Understanding context is equally vital because it plays an essential role in how you access and manipulate data in your JavaScript programs. Knowing when to use this
, as well as understanding how closures and scope work, can enhance the robustness of your code.
By mastering both const
and context in JavaScript, you'll write cleaner and more efficient code that is easier to debug and maintain.
17/11/2024 | VanillaJS
17/11/2024 | VanillaJS
17/11/2024 | VanillaJS
17/11/2024 | VanillaJS
18/11/2024 | VanillaJS
29/10/2024 | VanillaJS
29/10/2024 | VanillaJS